0

I have here a date and time and I'm wondering its outputting an incorrect date and time. 2022-05-19T21:53:00+00:00. Its currently outputting May 20. It should be May 19 still?

CODESANDBOX: CLICK HERE

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

let date = "2022-05-19T21:53:00+00:00";

document.getElementById("app").innerHTML = `
<div>${format(parseISO(date), "LLL dd, y hh:mm bbb")}</div>
`;
Joseph
  • 7,042
  • 23
  • 83
  • 181
  • It depends on your timezone. If you want it output as UTC you need to specify, otherwise it will be displayed in the system's timezone. (Displays `May 19, 2022 10:53 pm` for me in UTC+1) see: [date-fns | How do I format to UTC](https://stackoverflow.com/questions/58561169/date-fns-how-do-i-format-to-utc) – pilchard May 20 '22 at 00:58
  • @pilchard. Thanks. I wanted output it as whats on the given. I'm confused on what to follow on the link you provided. Can you put your answer below? Thank you – Joseph May 20 '22 at 01:27

1 Answers1

0

You don't actually need date-fns you can simply use a native Date with toLocaleDateString().

let date = "2022-05-19T21:53:00+00:00";

const options = {
  month: 'long',
  day: 'numeric',
  year: 'numeric',
  hour: 'numeric',
  minute: 'numeric',
  timeZone: 'UTC'
};

const dateLocaleString = new Date(date).toLocaleDateString('en', options);

document.getElementById("app").innerHTML = `
<div>${dateLocaleString}</div>
`;
<div id="app"></div>
pilchard
  • 12,414
  • 5
  • 11
  • 23