<div class="codetitle"> <span><a style="CURSOR: pointer" data="30631" class="copybut" id="copybut30631" onclick="doCopy('code30631')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code30631"> <br> <br> <br> <br><title>DST Calculator</title> <br><script type="text/javascript"> <br><br>function DisplayDstSwitchDates() <BR>{ <BR>var year = new Date().getYear(); <BR>if (year < 1000) <BR>year += 1900; <br><br>var firstSwitch = 0; <BR>var secondSwitch = 0; <BR>var lastOffset = 99; <br><br>// Loop through every month of the current year <BR>for (i = 0; i < 12; i++) <BR>{ <BR>// Fetch the timezone value for the month <BR>var newDate = new Date(Date.UTC(year, i, 0, 0, 0, 0, 0)); <BR>var tz = -1 * newDate.getTimezoneOffset() / 60; <br><br>// Capture when a timzezone change occurs <BR>if (tz > lastOffset) <BR>firstSwitch = i-1; <BR>else if (tz < lastOffset) <BR>secondSwitch = i-1; <br><br>lastOffset = tz; <BR>} <br><br>// Go figure out date/time occurences a minute before <BR>// a DST adjustment occurs <BR>var secondDstDate = FindDstSwitchDate(year, secondSwitch); <BR>var firstDstDate = FindDstSwitchDate(year, firstSwitch); <br><br>if (firstDstDate == null && secondDstDate == null) <BR>return 'Daylight Savings is not observed in your timezone.'; <BR>else <BR>return 'Last minute before DST change occurs in ' + <BR>year + ': ' + firstDstDate + ' and ' + secondDstDate; <BR>} <br><br>function FindDstSwitchDate(year, month) <BR>{ <BR>// Set the starting date <BR>var baseDate = new Date(Date.UTC(year, month, 0, 0, 0, 0, 0)); <BR>var changeDay = 0; <BR>var changeMinute = -1; <BR>var baseOffset = -1 * baseDate.getTimezoneOffset() / 60; <BR>var dstDate; <br><br>// Loop to find the exact day a timezone adjust occurs <BR>for (day = 0; day < 50; day++) <BR>{ <BR>var tmpDate = new Date(Date.UTC(year, month, day, 0, 0, 0, 0)); <BR>var tmpOffset = -1 * tmpDate.getTimezoneOffset() / 60; <br><br>// Check if the timezone changed from one day to the next <BR>if (tmpOffset != baseOffset) <BR>{ <BR>var minutes = 0; <BR>changeDay = day; <br><br>// Back-up one day and grap the offset <BR>tmpDate = new Date(Date.UTC(year, month, day-1, 0, 0, 0, 0)); <BR>tmpOffset = -1 * tmpDate.getTimezoneOffset() / 60; <br><br>// Count the minutes until a timezone chnage occurs <BR>while (changeMinute == -1) <BR>{ <BR>tmpDate = new Date(Date.UTC(year, month, day-1, 0, minutes, 0, 0)); <BR>tmpOffset = -1 * tmpDate.getTimezoneOffset() / 60; <br><br>// Determine the exact minute a timezone change <BR>// occurs <BR>if (tmpOffset != baseOffset) <BR>{ <BR>// Back-up a minute to get the date/time just <BR>// before a timezone change occurs <BR>tmpOffset = new Date(Date.UTC(year, month, <BR>day-1, 0, minutes-1, 0, 0)); <BR>changeMinute = minutes; <BR>break; <BR>} <BR>else <BR>minutes++; <BR>} <br><br>// Add a month (for display) since JavaScript counts <BR>// months from 0 to 11 <BR>dstDate = tmpOffset.getMonth() + 1; <br><br>// Pad the month as needed <BR>if (dstDate < 10) dstDate = "0" + dstDate; <br><br>// Add the day and year <BR>dstDate += '/' + tmpOffset.getDate() + '/' + year + ' '; <br><br>// Capture the time stamp <BR>tmpDate = new Date(Date.UTC(year, month, <BR>day-1, 0, minutes-1, 0, 0)); <BR>dstDate += tmpDate.toTimeString().split(' ')[0]; <BR>return dstDate; <BR>} <BR>} <BR>} <br><br></script> <br> <br> <br><script type="text/javascript"> <BR>document.write("Current date/time: " + new Date() + "<br />"); <BR>document.write(DisplayDstSwitchDates()); <BR></script> <br> <br> <br> </div>