0

I'm trying to create a countdown using dayjs, with a unix timestamp string, but it has to be according to a specific timezone (in this case its EST), this is my current code:

  //Timestamp - 1645631100000
  const formattedEndTime = dayjs.utc(1645631100000).format();

  const total =
    Date.parse(formattedEndTime) -
    Date.parse(dayjs().tz("America/New_York").format());

  const seconds = Math.floor((total / 1000) % 60);
  const minutes = Math.floor((total / 1000 / 60) % 60);
  const hours = Math.floor((total / (1000 * 60 * 60)) % 24);
  const days = Math.floor(total / (1000 * 60 * 60 * 24));

  return {
    total,
    days,
    hours,
    minutes,
    seconds,
  };

It is not giving me the right result, its always a few hours off, would appreciate any help with this, thanks!

  • Why are you comparing UTC to EST? They are always going to be off by the offset defined for EST from UTC, by definition. Also, it makes no sense to format a date to a string, then parse it as a date. Just take it as a date without the format/parse steps. – Heretic Monkey Feb 22 '22 at 17:34
  • Does this answer your question? [JavaScript - Get minutes between two dates](https://stackoverflow.com/questions/7709803/javascript-get-minutes-between-two-dates) – Heretic Monkey Feb 22 '22 at 17:34

0 Answers0