As a post-90s person, I always thought that China did not have the concept of daylight saving time, until I encountered a problem in the last project.

The problem is that the customer records the date of birth as 1989-05-23 in the management background, but finds the date as 1989-05-22 in ios. Because the time stamp used by the front and back ends to transmit the time, it is formatted as YYYY-MM-DD HH: MM :ss on ios and printed as 1989-05-22 23:00:00. On Both Android and Chrome, it’s 1989-05-23 00:00:00. That’s a good hour less.

In China, the average time is less than 8 hours, which may be due to the difference between international standard time and Beijing time, but an hour is rare. However, I did an international project before, and there was a difference called daylight saving time and winter time for European and American users, which happened to be an hour before and after adjustment. Could it be daylight saving time?

So I tried it on Chrome and Safari:


// chrome
new Date(1989.4.23) // Tue May 23 1989 00:00:00 GMT+0900

// safari
new Date(1989.4.23) // Tue May 23 1989 00:00:00 GMT+0800 (CST) = $6

Copy the code

You can see that Chrome automatically converts daylight saving time for the local time zone, which is GMT+0900 and Safari GMT+0800.

It turns out that China implemented daylight saving time for a period of time from 1986 to 1992:

In April 1986, the relevant department of the Central Government of China issued a “nationwide implementation of daylight Saving Time notice”, the specific approach is: every year from the middle of April on the first Sunday at 2 o ‘clock in the morning (Beijing time), the clock forward one hour, that is, the hands from 2 to 3, daylight saving time; By 2am on the first Sunday in mid-September (Beijing Daylight Saving Time), the clocks will be put back one hour, that is, the hands will be moved from 2am to 1am, the daylight saving time will end. From 1986 to 1991, except 1986, the first year of daylight saving time, from May 4 to September 14, the other years are in accordance with the provisions of the period of time. In the days leading up to the start and end of daylight saving time, the news media published announcements from the authorities. Daylight saving time was suspended in 1992.

So converting to a timestamp in Chrome itself loses an hour.

// chrome
new Date(1989.4.23).getTime() / / 611852400000

// safari
new Date(611852400000) // Mon May 22 1989 23:00:00 GMT+0800 (CST) = $7

Copy the code

When we consume this timestamp, it is difficult to determine what the original time is. Only for the special case of birthday, because the upload time must be the zero time of a certain day, so when the detection is 23 o ‘clock, we can add an hour, but except for this special case, we can not deal with it.

To fix this problem, we need to do something about daylight saving time when we produce this timestamp. For example, check whether new Date().toString() contains daylight saving time or +0900. If yes, daylight saving Time (DST) is switched. At this time, we need to add the time of 1h on the basis of the original timestamp. Or use momentjs to detect whether daylight saving time conversion function can also be determined.