Js obtains the time code of occurrence and termination in the current time zone.

Source: Internet
Author: User

Js obtains the time code of occurrence and termination in the current time zone.

This article mainly introduces the time code for js to obtain the occurrence and termination of the token in the current time zone. For more information, see

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 <1, 1000)

Year + = 1900;

 

Var firstSwitch = 0;

Var secondSwitch = 0;

Var lastOffset = 99;

 

// Loop through every month of the current year

For (I = 0; I <12; I ++)

{

// Fetch the timezone value for the month

Var newDate = new Date (Date. UTC (year, I, 0, 0, 0, 0, 0 ));

Var tz =-1 * newDate. getTimezoneOffset ()/60;

 

// Capture when a timzezone change occurs

If (tz> lastOffset)

FirstSwitch = I-1;

Else if (tz <lastOffset)

SecondSwitch = I-1;

 

LastOffset = tz;

}

 

// Go figure out date/time occurences a minute before

// A DST adjustment occurs

Var secondDstDate = FindDstSwitchDate (year, secondSwitch );

Var firstDstDate = FindDstSwitchDate (year, firstSwitch );

 

If (firstDstDate = null & secondDstDate = null)

Return 'daylight Savings is not observed in your timezone .';

Else

Return 'last minute before DST change occurs in '+

Year + ':' + firstDstDate + 'and' + secondDstDate;

}

 

Function FindDstSwitchDate (year, month)

{

// Set the starting date

Var baseDate = new Date (Date. UTC (year, month, 0, 0, 0, 0, 0 ));

Var changeDay = 0;

Var changeMinute =-1;

Var baseOffset =-1 * baseDate. getTimezoneOffset ()/60;

Var dstDate;

 

// Loop to find the exact day a timezone adjust occurs

For (day = 0; day <50; 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 offset

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)

{

TmpDate = new Date (Date. UTC (year, month, day-1, 0, minutes, 0, 0 ));

TmpOffset =-1 * tmpDate. getTimezoneOffset ()/60;

 

// 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 (year, 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 <10) dstDate = "0" + dstDate;

 

// Add the 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>

<Script type = "text/javascript">

Document. write ("Current date/time:" + new Date () + "<br/> ");

Document. write (DisplayDstSwitchDates ());

</Script>

</Body>

</Html>

 

 

Related Article

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.