0

(Just wondering about this answer being a duplicate? I see the other answers are NOT asking for the result in Milliseconds???)

Was simply trying to fill in dates between start and end date:

<div id="my_div">

</div>

<script>
    var START_DATE_MILLISECONDS = new Date("03/12/2017").getTime();
    var END_DATE_MILLISECONDS = new Date("03/17/2017").getTime();
    var HRS_24_Converted_To_Millisecs = 86400000; //24h

    for(var i=START_DATE_MILLISECONDS; i<=END_DATE_MILLISECONDS; i+=HRS_24_Converted_To_Millisecs){

            var i_converted = new Date(i);
            document.getElementById('my_div').innerHTML += "~ i ~ ="+i+" -> "+i_converted+"<BR>";

            var END_DATE_MILLISECONDS = new Date(END_DATE_MILLISECONDS);
            document.getElementById('my_div').innerHTML += "~ END_DATE_MILLISECONDS ~ ="+END_DATE_MILLISECONDS+" ->"+END_DATE_MILLISECONDS+" <br>";

    }
    document.getElementById('my_div').innerHTML += "<BR><BR>";
        var i_converted = new Date(i);

        document.getElementById('my_div').innerHTML +=(" \n\n ______________________________ <BR>EXIT LIOOP IAND i = "+i+" -><BR><span style='color:red;'>"+i_converted+"</span><BR>__________________________________________ <BR>");
</script>

https://jsfiddle.net/kdfiddle/dfLh04qc/

  • The dates should loop through and include the 17th - but it is skipped because the for loop reaches a value greater - by one hour - then the last day 17th.

  • This only happens if the 13th day of March (spooky 0_o) is included in the calculation. Anything before - or after seems to work properly.

I suspect the UTC is the issue - but am not certain what to do about it. Is the ++ 86400000 a good idea for incrementing dates? Something that works more... "fool" proof?

  • 2
    One day is not equal to 86400000 milliseconds. Sometimes, it is an entire hour longer or shorter. This frankly bizarre phenomenon happens one each per year, but varies depending on where you happen to live. Some places don't have it at all. Sometimes, the country decides that one of the two won't happen, resulting in a permanent change in time zone. This is Daylight Savings Time, the most baffling decision I've encountered regarding timekeeping. – Niet the Dark Absol Jun 20 '17 at 22:37
  • 1
    To add a day to a date, keep a single Date object (eg. `var dateObj = new Date(start)`) and increment its date field: `dateObj.setDate(dateObj.getDate()+1)` - this will automatically wrap between months for you, by the way, and even consider leap years for you. – Niet the Dark Absol Jun 20 '17 at 22:38
  • Thanks for the input peopel - I appreciate it. I can't believe I didn't find that answer - i searched like crazy! 0_o –  Jun 20 '17 at 22:57
  • Just wondering about this answer being a duplicate? I see the other answers are not asking for the result in Milliseconds??? –  Jun 20 '17 at 23:05
  • 1
    Just increment by a day (as described in the linked answer) and then do `.getTime()` on the result. – Clonkex Jun 20 '17 at 23:10
  • What does stack want you to do with these duplicate questions? Delete them? –  Jun 20 '17 at 23:39

0 Answers0