As you can see, with moment lib, we need 2 steps to get the result: parse string to Date object, then format date object to string.
Your code - format(new Date("10-13-20"), 'MM-DD-YY') is format step, try convert a date object to a string with format template is MM-DD-YY. But your date object is not correct.
The solution is to do the same as with moment lib:
Parse date string to date object. Use parse
const dateString = '10-13-20'; const date = parse(dateString, 'MM-dd-yy', new Date()) // not MM-DD-YYFormat date object to result string. Use format
const result = format(date, "yyyy-MM-dd'T'HH:mm:ss.SSSxxx") console.log(result)Result will be like (the same with moment's result in my timezone):
2020-10-13T00:00:00.000+09:00
As you can see, with moment lib, we need 2 steps to get the result: parse string to Date object, then format date object to string.
Your code - format(new Date("10-13-20"), 'MM-DD-YY') is format step, try convert a date object to a string with format template is MM-DD-YY. But your date object is not correct.
The solution is to do the same as with moment lib:
Parse date string to date object. Use parse
const dateString = '10-13-20'; const date = parse(dateString, 'MM-dd-yy', new Date()) // not MM-DD-YYFormat date object to result string. Use format
const result = format(date, "yyyy-MM-dd'T'HH:mm:ss.SSSxxx") console.log(result)Result will be like (the same with moment's result in my timezone):
2020-10-13T00:00:00.000+09:00
const date = "2021-12-20"
console.log(format(parseISO(date), "dd-MM-yyyy"));
// output: 20-12-2021
Videos
» npm install date-fns
» npm install date-fns-tz
I use the esm version of date-fns and you can use the same type of formats that moment uses :
import { format } from 'date-fns/esm'
import { enUS, fr } from 'date-fns/esm/locale'
I'll store the locales in an object :
this.dateLocales = { fr: fr, en: enUS }
and use these formats :
LT: 'h:mm aa',
LTS: 'h:mm:ss aa',
L: 'MM/DD/YYYY',
LL: 'MMMM D YYYY',
LLL: 'MMMM D YYYY h:mm aa',
LLLL: 'dddd, MMMM D YYYY h:mm aa'
So you can do :
format(
new Date(2014, 6, 2),
'LL',
{locale: this.dateLocales.fr}
)
Those formats are localised
As of 2021, using currently latest version of date-fns (v2.23.0), you can achieve what you want by using the 'P' format.
See: https://date-fns.org/v2.23.0/docs/format
For example, given that today is 2021-08-27 (ISO date):
import { format } from 'date-fns';
import ptBrLocale from 'date-fns/locale/pt-BR';
import enUsLocale from 'date-fns/locale/en-US';
console.log(format(new Date(), 'P', { locale: ptBrLocale }));
console.log(format(new Date(), 'P', { locale: enUsLocale }));
Outputs will be:
27/08/2021
08/27/2021
It seems that you are using Version 2.0 of date-fns, which is still in alpha (development) status currently.
What functions are available and how they work still seems to change frequently. For example, while in version v2.0.0-alpha.26 toDate() can handle string parameters, it cannot do that any longer in version v2.0.0-alpha.27. There is a new parseISO() function instead.
This should work now:
format(parseISO('2019-02-11T14:00:00'), 'MM/dd/yyyy')
However, while version 2 is still in beta, I would suggest using the stable version 1.x for now.
date-fns 2.0.0-alpha.27 (demo: https://stackblitz.com/edit/js-tztuz6)
Use parseISO:
import { format, parseISO } from 'date-fns'
const formattedDate = format(parseISO('2019-02-11T14:00:00'), 'MM/dd/yyyy');
date-fns v1.30.1 (demo: https://stackblitz.com/edit/js-mt3y6p)
Use parse:
import { format, parse } from 'date-fns'
const formattedDate = format(parse('2019-02-11T14:00:00'), 'MM/DD/YYYY');