我的脚本如何检测Day节能是否开始

时间:2017-11-06 08:10:04

标签: javascript jquery

我创建了这个脚本,它显示了Danville Va的正确时间,直到昨天,但DST昨天结束了Danville Va,

  

DST于2017年3月12日星期日,当地标准时间(EST)02:00开始

     

DST于2017年11月5日星期日,当地日光时间(EDT)02:00结束

我可以将脚本更改为renderTime(-4)renderTime(-5)它会起作用,但如何检测以便在DST开始或结束时自动显示正确的时间,我不想使用任何外部库



function renderTime(offset) {
    var d = new Date();
            localTime = d.getTime();
            localOffset = d.getTimezoneOffset() * 60000;

            // obtain UTC time in msec
            utc = localTime + localOffset;
            // create new Date object for different city
            // using supplied offset
            var currentTime = new Date(utc + (3600000*offset));


			var diem = "AM";
			var y = currentTime.getFullYear().toString().substr(-2);
			var mo = currentTime.getMonth() + 1;
			var day = currentTime.getDate();
			var h = currentTime.getHours();
			var m = currentTime.getMinutes();
			var s = currentTime.getSeconds();


			if (h == 0) {
			    h = 12
			} else if (h > 12) {
			    h = h - 12;
			    diem = "PM";
			}

			if (h < 10) {
			    h = "0" + h;
			}

			if (m < 10) {
			    m = "0" + m;
			}

			if (s < 10) {
			    s = "0" + s;
			}

			$('#date').text(mo + "/" + day + "/" + y);
			$('#time').text( h + ":" + m + ":" + s + " " + diem);
			
          }

$(document).ready(function(){
		renderTime(-4);
	  setInterval('renderTime(-4)', 1000);

     });
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="info list-unstyled d-flex flex-column align-items-center" style="position: fixed; bottom: 1em; right: 4em;">
  <li>[DANVILLE,VA]</li>
  <!-- Date format is Month/Day/Year -->
  <li id="date"></li>
  <li id="time"></li>
</ul>
  
&#13;
&#13;
&#13;

更新

它应该向世界上的每个人显示同一时间(丹维尔的时间),它不应该取决于用户的位置。我不想使用Moment.js

由于

1 个答案:

答案 0 :(得分:1)

嗯,这并不难,但是如果你需要一个完整的解决方案就好了,希望它有所帮助!

Date.prototype.stdTimezoneOffset = function() {
    var jan = new Date(this.getFullYear(), 0, 1);
    var jul = new Date(this.getFullYear(), 6, 1);
    return Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset());
};

function renderTime(offset) {
    var d = new Date(),
        localTime   = d.getTime(),
        localOffset = d.stdTimezoneOffset() * 60000,
        utc         = localTime + localOffset,
        currentTime = new Date(utc + (3600000 * offset)),
        y   = currentTime.getFullYear().toString().substr(-2),
        mo  = currentTime.getMonth() + 1,
        day = currentTime.getDate(),
        h   = currentTime.getHours(),
        m   = currentTime.getMinutes(),
        s   = currentTime.getSeconds(),
        diem = (h > 12) ? "PM" : "AM";

    if (h == 0) {
        h = 12;
    } else if (h > 12) {
        h = h - 12;
    }

    if (h < 10) h = "0" + h;
    if (m < 10) m = "0" + m;
    if (s < 10) s = "0" + s;

    $('#date').text(mo + "/" + day + "/" + y);
    $('#time').text(h + ":" + m + ":" + s + " " + diem);
}

$(document).ready(function() {
    setInterval(function() {
        renderTime(-4);
    }, 1000);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="info list-unstyled d-flex flex-column align-items-center" style="position: fixed; bottom: 1em; right: 4em;">
    <li>[DANVILLE,VA]</li>
    <!-- Date format is Month/Day/Year -->
    <li id="date"></li>
    <li id="time"></li>
</ul>