Search code examples
javascriptdateutc

set / get VS setUTC / getUTC


I'm working with dates and I was wondering if there was a border case where:

var d2 = new Date(d);
d.setHours(d.getHours() + n) !== d2.setUTCHours(d2.getUTCHours() + n)

..?


Solution

  • In the US this year (2016), Daylight Savings time starts at 2:00AM on Sunday 13 March:

    var dstDate = new Date(2016, 2, 13);
    var dstUTC = new Date(dstDate);
    

    If we add 3 hours as per your question:

    dstDate.setHours(dstDate.getHours() + 3);
    console.log(dstDate); // 3:00AM
    
    dstUTC.setUTCHours(dstUTC.getUTCHours() + 3);
    console.log(dstUTC); // 4:00 AM
    

    (My local time zone is UTC-6.)