Skip to content

Date .format behind by one day #2741

Answered by fturmel
cdt-eth asked this question in Q&A
Nov 1, 2021 · 1 comments · 2 replies
Discussion options

You must be logged in to vote

Yeah, date-fns works with local time zones only at the moment. The problem is you're passing strings in the JS Date constructor which treats the input as UTC, but the date-fns functions work on local time zone representations of the date.

[...] date-only strings (e.g. "1970-01-01") are treated as UTC, not local
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date#parameters

import { format, parseISO } from "date-fns";

console.log(format(parseISO("2021-11-01"), "MM/dd/yyyy")); // 11/01/2021
console.log(format(new Date(2021, 10, 1), "MM/dd/yyyy"));  // 11/01/2021

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@fturmel
Comment options

@cdt-eth
Comment options

Answer selected by fturmel
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