.format adds 1 hour to time I pass it #3440
-
I'm using node js and am trying to make a reservation system. I'm passing the datetime as 2023-06-06T12:00:00.000Z and it is being stored correctly on my database. However, I'm trying to send confirmation and cancelation emails to each user creating or removing a reservation and I don't want to send an email saying "Reservation for 2023-06-06T12:00:00.000Z confirmed".
When I console log this formattedReservationHour or received the email, for some reason date-fns is adding 1 hour to the time I pass it (instead of 12:00, it turns into 13:00). How can I fix this? Please let me know if you can help in some way. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I appreciate this was asked a while ago but I got tripped up by this recently. The To force date-fns to stick to UTC and not perform this conversion we need to use a date object from date-fns/utc. Example: import { UTCDateMini } from '@date-fns/utc';
import { format } from 'date-fns/format';
const veryGoodFunctionName = (date: Date): string => format(new UTCDateMini(date), "HH:mm"); |
Beta Was this translation helpful? Give feedback.
I appreciate this was asked a while ago but I got tripped up by this recently. The
format
function seems to output using the timezone of the runtime device. So for example, if you are in British Summer Time (UTC+1) and passnew Date('2023-06-06T12:00:00.000Z')
intoformat
, you'll get out a timestamp that is one hour ahead -2023-06-06T13:00:00.000Z
. I'm sure there's an interesting reason for this but I've not taken the time to look into it.To force date-fns to stick to UTC and not perform this conversion we need to use a date object from date-fns/utc. Example: