Solution to calculation error of Daylight Saving Time during Daylight Saving Time

Source: Internet
Author: User

After several days of struggle, I finally found a solution today. Please share it with me:

The time zone may occur during the renewal:

1. non-renewal:

2. Renewal:

3. Code:

It is also a subtraction of two times. Why is the daylight saving time different from the non-Daylight Saving Time? This is actually the rule of daylight saving time. At the end of March every year, the system clock will automatically Jump faster than an hour during the hour, the specific operation is that when the clock goes to, the next minute is; if the clock is switched to non-clock, it is generally in early November, when the clock goes to, the next moment is.

The two times I calculated above actually pass through the hour. If you calculate it carefully (181-180,958 33333 ..) * 24. The result is exactly one hour, that is, the computing duration is less than one hour during the Failover. This is the root cause of program errors. Find the cause and find a solution.

4. solution:

/// <summary>/// Fixed TimeZone issue: Transfer the sDate1 and sDate2 to UTC, then minus./// </summary>this.dateDiff = function (sDate1, sDate2){//var days = parseInt((sDate1 - sDate2) / 1000 / 60 / 60 / 24);var d1 = Date.UTC(sDate1.getFullYear(), sDate1.getMonth(), sDate1.getDate(), sDate1.getHours(), sDate1.getMinutes(), sDate1.getSeconds(), sDate1.getMilliseconds());var d2 = Date.UTC(sDate2.getFullYear(), sDate2.getMonth(), sDate2.getDate(), sDate2.getHours(), sDate2.getMinutes(), sDate2.getSeconds(), sDate2.getMilliseconds());var days = (d1 - d2) / 1000 / 60 / 60 / 24;return days;}

During time subtraction, find the UTC [also called GMT, Greenwich Mean] time corresponding to the current time, and then subtract it with two UTC time, the result will not change with the time zone.

If you have good methods or insights, please share them with me.


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.