JS Gets the time code of the current time zone daylight saving and expiration

Source: Internet
Author: User
Tags current time

  This article mainly introduces JS to obtain the current time zone daylight saving period and termination of the code, the need for friends can refer to the following

The code is as follows: <! DOCTYPE html>  <html>  <head>  <title>dst calculator</title>  < Script type= "Text/javascript" >    function displaydstswitchdates ()   {  var year = new Date (). GetYear ();  if (Year < 1000)   year = 1900;    var firstswitch = 0;  var secondswitch = 0;&nbs P var LastOffset = 99;   /Loop through every month of the current year  for (i = 0; i <; i++)   { //Fetch The timezone value for the month  var newdate = new Date (DATE.UTC (year, I, 0, 0, 0, 0, 0));  VA R TZ =-1 * newdate.gettimezoneoffset ()/60;   //Capture when a timzezone change occurs  if (TZ > Las Toffset)   Firstswitch = i-1;  else if (TZ < LastOffset)   Secondswitch = i-1;    LastOffset = t z; }   //Go figure out date/time occurences a minute before //a DST adjustment occurs  var s Econddstdate = FINDDSTSWItchdate (Secondswitch);  var firstdstdate = finddstswitchdate (year, firstswitch);    if ( Firstdstdate = = NULL && seconddstdate = null)   Return ' daylight savings are not observed in your timezone. '   else  return ' last minute before DST change occurs in ' +  year + ': ' + firstdstdate + ' and ' + secondd stdate; }    Function finddstswitchdate (year, month)   { //Set The starting date  var base Date = new Date (DATE.UTC (year, month, 0, 0, 0, 0, 0));  var changeday = 0;  var changeminute = -1;  var bas Eoffset =-1 * basedate.gettimezoneoffset ()/60;  var dstdate;   //Loop to find the exact day a timezone Adjust occurs  for (day = 0; day < day++)   {  var tmpdate = new Date (DATE.UTC (year, month, day, 0, 0, 0, 0);  var tmpoffset =-1 * tmpdate.gettimezoneoffset ()/60;   //Check If the timezone changed from One day to the next  if (tmpoffSet!= baseoffset)   {  var minutes = 0;  Changeday = day;   //back-up one day and grap the offs et  tmpdate = new Date (DATE.UTC (year, month, day-1, 0, 0, 0, 0));  tmpoffset =-1 * tmpdate.gettimezoneoffset () /60;   //Count The minutes until a timezone chnage occurs  while (Changeminute = 1)   {  TmpD ate = new Date (DATE.UTC (year, month, Day-1, 0, minutes, 0, 0));  tmpoffset =-1 * tmpdate.gettimezoneoffset ()/60;&N Bsp  //Determine the exact minute a timezone change //occurs  if (tmpoffset!= baseoffset)   { // Back-up a minute to get the Date/time just //Before a timezone change occurs  tmpoffset = new Date (DATE.UTC (y Ear, month,  day-1, 0, minutes-1, 0, 0);  Changeminute = minutes;  break; }  else -minutes ++; }   //ADD a month (for display) since JavaScript counts //months from 0 to 11  dstdate = Tmpoffset.getmonth() + 1;   /Pad the month as needed  if (Dstdate < ten) Dstdate = "0" + dstdate;   //ADD th E Day and year  dstdate + = '/' + tmpoffset.getdate () + '/' + year + ';   //Capture the time stamp  Tmpdate = new Date (DATE.UTC (year, month,  day-1, 0, minutes-1, 0, 0));  dstdate + = Tmpdate.totimestring (). Split (' ') [0];  return dstdate; } } }    </script>  </head>  <body>& nbsp <script type= "Text/javascript" >  document.write ("Current date/time:" + new Date () + "<br/>");  do Cument.write (Displaydstswitchdates ());  </script>  </body>  </html>     

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.