Convert new Date() to weekday string eg 'Thursday' using date-fns

21.4k views Asked by At

I'm using date-fns I would like to convert new Date(2020,1,10) to 'Thursday'. I'm trying to use

dateFns.format(new Date(2020,1,10),'dddd')

but it returns 0001?

Thanks in advance

4

There are 4 answers

2
Nauman Tahir On BEST ANSWER

Use this for full Day name:

import {format} from 'date-fns';

format(new Date(2020,1,10), 'EEEE')
0
Amin Dulkumoni On

This also works: format(new Date(2020,1,10), 'iiii')

0
atazmin On

For me using date ("2022-10-29") and format(new Date(date), 'EEEE') showed wrong weekday Friday instead of Saturday (I am Central Time), from my understanding I need to provide time to get correct weekday.

for some reason this using different "Month Day, Year" format this worked: {format(parseISO(date), "MMMM d, yyyy")} => October 29, 2022

Since I had time in different variable, I have combined, date and time {format(new Date(date + "T" + time + "Z"), "EEEE")} this seem to work, shows correct weekday: Saturday

0
Green On

"dddd" only works in older versions of date-fns (< 2.0.0) for getting the day of the week. In newer versions, "dddd" is replaced with "EEEE".