My old code use momentjs, something like this :
moment(createdAt, 'YYYY-MM-DD HH:mm:ss').toISOString()
What is the equivalent method on date-fns that will reproduce same result ?
My old code use momentjs, something like this :
moment(createdAt, 'YYYY-MM-DD HH:mm:ss').toISOString()
What is the equivalent method on date-fns that will reproduce same result ?
Try this,
new Date(createAt).toISOString()
date-fns has a function: formatISO()
So, if you want to stay with date-fns you can write:
formatISO(new Date())
Currently date fns formatISO does not support miliseconds, https://github.com/date-fns/date-fns/issues/2968
Consequence:
date.toISOString() != dateFns.formatISO(date)
You should better use js native toISOString() (ECMAScript5)
If not available a polyfill could be used
if ( !Date.prototype.toISOString ) {
( function() {
function pad(number) {
if ( number < 10 ) {
return '0' + number;
}
return number;
}
Date.prototype.toISOString = function() {
return this.getUTCFullYear() +
'-' + pad( this.getUTCMonth() + 1 ) +
'-' + pad( this.getUTCDate() ) +
'T' + pad( this.getUTCHours() ) +
':' + pad( this.getUTCMinutes() ) +
':' + pad( this.getUTCSeconds() ) +
'.' + (this.getUTCMilliseconds() / 1000).toFixed(3).slice(2, 5) +
'Z';
};
}() );
}
© 2022 - 2025 — McMap. All rights reserved.