2

I created this script it was showing the correct time in Danville Va, till yesterday, but the DST ended yesterday for Danville Va,

DST started on Sunday 12 March 2017, 02:00 local standard time (EST)

DST ends on Sunday 05 November 2017, 02:00 local daylight time (EDT)

I can change the script to renderTime(-4) to renderTime(-5) It will work, But how can detect so that it automatically show the correct time when DST starts or ends, I don't wanna use any external library

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);

     });
<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>
  

UPDATE

It should show the same time (Danville's time) to everyone in the world, It should not be dependent on where the user is. I don't wanna use Moment.js

Thanks

johnDoe
  • 63
  • 2
  • 8
  • @AngYC mine is more specific, https://stackoverflow.com/questions/11887934/how-to-check-if-the-dst-daylight-saving-time-is-in-effect-and-if-it-is-whats is very general, I want to do this only for Danville, So the code should be more specific and easier, I think – johnDoe Nov 06 '17 at 08:13

1 Answers1

1

Well, it is not that hard but it is fine if you need a complete solution, hope it helps!

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>
AngYC
  • 3,051
  • 6
  • 20
  • Thanks, But time is still incorrect – johnDoe Nov 06 '17 at 08:23
  • how can i use that function in my script, i am not familiar with javascript prototype – johnDoe Nov 06 '17 at 08:24
  • The problem here is, I am not living in any DST countries so I don't really have a clear idea (I will try to fix later). However, I would really recommend Moment.js and it is very reliable to deal with time/date – AngYC Nov 06 '17 at 08:26
  • It should show the same time (Danville's time) to everyone in the world, It should not be dependent on where the user is. I don't wanna use Moment.js.Thanks :) – johnDoe Nov 06 '17 at 08:29
  • Hi, I created the correct version for Danville check this https://jsfiddle.net/6wrm0L1k/1/ – johnDoe Nov 07 '17 at 08:21