How to decode this time string [duplicate]

I got this string from an api After looking at it I realized it was a dat/time

20220112201146 

I then decoded it by hand to be

2022(Y)01(M)12(D)20(H)11(M)46(S) 

How would I slice everything up to be Y:M:D:H:M:S? Example:

2022:01:12:20:11:46 

And then add 80 mins to it?

4

1 Answer

Extract the various parts (year, month, day, etc) via regex, transform it to ISO 8601 format, parse it to a Date instance, then add 80 minutes

const str = "20220112201146" const rx = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/ const iso8601 = str.replace(rx, "$1-$2-$3T$4:$5:$6") console.log("iso8601:", iso8601) const date = new Date(iso8601) console.log("original date:", date.toLocaleString()) date.setMinutes(date.getMinutes() + 80) console.log("future date:", date.toLocaleString())
1

You Might Also Like