Skip to content

.format adds 1 hour to time I pass it #3440

Discussion options

You must be logged in to vote

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 pass new Date('2023-06-06T12:00:00.000Z') into format, 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:

import { UTCDateMini } from '@date-fns/utc';
import { format } from 'date-fns/format';

const veryGoodFunctionName = (date: Date): 

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by danielmsrodrigues
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants