My pany is using date-fns and I'm trying to set a variable equal to the current date + time. We must use date-fns, and the format is specified to be 'YYYY-MM-DDThh:mm:ss.dddddd'.
How can I do this? I tried simply format(new Date(), 'YYYY-MM-DDThh:mm:ss:dddddd')
, but that was not working as expected.
(Also what is the 'T' for? Just to specify that a time measurement will follow? And what are the d's?)
Thanks!!
My pany is using date-fns and I'm trying to set a variable equal to the current date + time. We must use date-fns, and the format is specified to be 'YYYY-MM-DDThh:mm:ss.dddddd'.
How can I do this? I tried simply format(new Date(), 'YYYY-MM-DDThh:mm:ss:dddddd')
, but that was not working as expected.
(Also what is the 'T' for? Just to specify that a time measurement will follow? And what are the d's?)
Thanks!!
Share Improve this question edited Aug 4, 2021 at 22:21 Arvind Kumar Avinash 79.8k10 gold badges92 silver badges135 bronze badges asked Aug 3, 2021 at 17:12 tprebendatprebenda 5142 gold badges8 silver badges22 bronze badges1 Answer
Reset to default 5Your pattern is not correct e.g. you need
y
instead ofY
d
instead ofD
H
instead ofh
etc. Check this page to learn more about these letters.
Demo:
const fns = require('date-fns')
console.log(fns.format(new Date(), "yyyy-MM-dd'T'HH:mm:ss.SSS"))
Output from a sample run:
2021-08-03T19:43:08.891
ONLINE DEMO
(Note: Click Console
on the bottom right of the ONLINE DEMO page.)
(Also what is the 'T' for? Just to specify that a time measurement will follow?
Correct. It is a separator for date and time as per ISO-8601 standards.