I managed to run it successfully by using require as shown below:
const fns = require('date-fns')
console.log(fns.format(new Date(), "'Today is a' eeee"))
Update:
I installed node v16.6.1 following the instructions in this answer and now I can run the following code successfully:
import { format } from 'date-fns';
console.log(format(new Date(), "yyyy-MM-dd'T'HH:mm:ss.SSS"));
Answer from Arvind Kumar Avinash on Stack Overflow
ยป npm install date-fns
Videos
I managed to run it successfully by using require as shown below:
const fns = require('date-fns')
console.log(fns.format(new Date(), "'Today is a' eeee"))
Update:
I installed node v16.6.1 following the instructions in this answer and now I can run the following code successfully:
import { format } from 'date-fns';
console.log(format(new Date(), "yyyy-MM-dd'T'HH:mm:ss.SSS"));
You are probably facing syntax errors, as you directly copy pasted the code from the documentation. Try importing the library as follows. It should work just fine.
import { format, formatDistance, formatRelative, subDays } from 'date-fns';
const mDate= format(new Date(2014, 1, 11), 'MM/dd/yyyy');
console.log(mDate);
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