0

Im sure this topic has been beaten to a pulp but I havent yet been able to find the solution. The library Im using (fullcalendar) changes the format of your dates and times after you have them displayed on the screen. If you use the eventClick function and log the start or end of an event the object looks something like this

Thu Jul 23 2020 23:59:00 GMT-0500 (Central Daylight Time)

Now I want to use this in a modal after an event has been clicked. During this function I need to run some checks on the date and time so I use JSON.stringify().

After I have done the above, my date now looks like this (notice the wrong date and time)

"2020-07-24T04:59:00.000Z"

Is there a non "hacky" way to solve this problem?

FamousAv8er
  • 2,345
  • 2
  • 9
  • 27
  • you should not stringify your date, you lose all the methods of the Date object – gui3 Jul 23 '20 at 19:52
  • 2
    There's no problem. The dates are equivalent. The "Z" in the second string refers to "Zulu", or "UTC". Since your original time was in Central Time, it adds 5 hours. – Heretic Monkey Jul 23 '20 at 19:52
  • Does this answer your question? [How to format a JavaScript date](https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date) – Heretic Monkey Jul 23 '20 at 19:55

1 Answers1

2

The time isn't wrong - the first is GMT-5, the second UTC.

When you JSON.Stringify, you get out the standard UTC date, but fullcalendar does some clever time-zone handling for you:

From the docs:

It’s important to always remember that a Date object is merely a wrapper around a millisecond time since the Unix Epoch (Jan 1, 1970 UTC). The accessor methods on the Date object interpret this millisecond time differently for different time zones, but the underlying millisecond time will remain the same.

You should use the methods given to you by that library to get info for your checks.

Luke Storry
  • 6,032
  • 1
  • 9
  • 22