Search code examples
javascriptjqueryasp.net-mvcdatetimeutc

setUTCHours Adds an hour to time passed to the function


I am trying to create a DateTime object from date and time strings separately. However setUTCHour function keeps on adding an hour to the time, I understand UK's time is GMT + 1 However as the time is already stored in UTC Time I don't want to add an hour to the time..

below is the code for this..

var eventStartDate = $('#EventStartDate').val();  // value:  "10/10/2016"
var eventStartTime = $('#EventStartTime').val();  // value:   "10:00"
var eventStartTimeArr = eventStartTime.split(":"); 

if (eventStartTimeArr.length = 2) {
    eventStart.setUTCHours(eventStartTimeArr[0], eventStartTimeArr[1], 00, 00)
}

This shows me event start time as "10/10/2016 11:00" which is one hour more then exprected value.. this should be "10/10/2016 10:00".


Solution

  • setUTCHours is basically setHours using the UTC offset.

    For example, if you were in the GMT-7 timezone, using setUTCHours(10, 0, 0, 0) would be equivalent to using setHours(10 + (-7 /* local time offset */), 0, 0, 0).

    If you have already set your time in UTC, you don't need to re-apply that offset (as the object's local time is now UTC time). Use setHours to directly apply the hours instead.