Reputation: 141
we are migrating from moment library to date-fns in the project on which I am working.
I am trying to find a soution to replace it with date-fns but I am not successful:
timezone: moment().format('Z')
If I replace it with:
timezone: format(new Date(),'Z')
Then It is not working. If anyone know how to solve it then please let me know. Thanks
Upvotes: 4
Views: 7427
Reputation: 7119
It can be done like this:
timezone: format(new Date(),'XXX')
It might be helpful for you, I change moment data in my project like this:
Moment data:
import moment, { Moment } from 'moment/moment';
const DATE_FORMAT = 'YYYY-MM-DDTHH:mm:ss[Z]';
moment().endOf('day').format(DATE_FORMAT);
moment().subtract(13, 'days');
moment().subtract(1, 'months');
moment().subtract(1, 'years');
moment().subtract(1, 'years').startOf('year');
moment().endOf('year').format(DATE_FORMAT);
function getStartDate(date: Moment): string {
return date.startOf('day').format(DATE_FORMAT);
}
To
date-fns data:
import {
endOfDay,
endOfYear,
format,
startOfDay,
startOfYear,
subDays,
subMonths ,
subYears } from 'date-fns';
const DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
format(endOfDay(new Date()), DATE_FORMAT);
subDays(new Date(Date.UTC(new Date().getUTCFullYear(), new Date().getUTCMonth(), new Date().getUTCDate())),
13);
subMonths(new Date(Date.UTC(new Date().getUTCFullYear(), new Date().getUTCMonth(), new Date().getUTCDate())),
1);
subYears(new Date(Date.UTC(new Date().getUTCFullYear(), new Date().getUTCMonth(), new Date().getUTCDate())),
1);
startOfYear(subYears(new Date(Date.UTC(new Date().getUTCFullYear(), new Date().getUTCMonth(), new Date().getUTCDate())),
1));
format(endOfYear(new Date()), DATE_FORMAT);
function getStartDate(date: Date): string {
return format(startOfDay(date), DATE_FORMAT);
}
Upvotes: 5