Sunday 31 May 2020

TimeStamp interesting

I had trouble in saving date into database and read it again.

received date: DateTime
collected date: TimeZone








In summary

  • I created all columns as TimeZone.
  • When read in js side, just do substring(0,10) to remove 10:00 hour time..
  • Show in chrome date type.
  • When saving, rely on whatever API does, and let it save with 10:00 hour time.


MySQL check current time zone (https://stackoverflow.com/questions/2934258/how-do-i-get-the-current-time-zone-of-mysql) shows below. And it seems my MYSQL follows my local machine time stamp - AEST.



So far so good.
The problem happens when reading the date value again in to javascript.





When reading the database value '2020-06-01 10:00 AEST' into API and into js, in some how, it becomes '2020-06-01 00:00 AEST'.

This leads js display incorrect giving 31/5 not 1/6.



The resolutions is, whatever date you have in javascript, you ignore the timezone, and just convert the value (2020-06-01 00:00 AEST) into string.



Javascript fun fact

Datetime is funny in java script. new Date(null) is 1970-1-1.







No comments:

Post a Comment