phonegapnoob
phonegapnoob

Reputation: 87

Javascript Date utcoffset and adding minutes

What am I doing wrong here ? I cannot get this to matchup with the current UTC time, after I enter my current time as userPickedTime.

userPickedTime = new Date();
userPickedTime.setHours(3,30,0);
userTimeChoiceConvertedToUtc  = new Date (userPickedTime.getTime() + 
    (3600000*userPickedTime.getTimezoneOffset()));

Upvotes: 1

Views: 550

Answers (2)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241563

While Raul is correct that the offset is expressed in minutes and you are using the wrong multiplier, you really shouldn't add the offset to the timestamp. The result will be a different point in time entirely.

The timestamp is already in terms of UTC, and the Date object constructor expects that you are passing in milliseconds from the epoch, also in terms of UTC.

You should just use the getUTC... functions, or the toUTCString, or toISOString functions instead.

If you desire more specific formatting of UTC values, consider using the moment.js library.

Upvotes: 0

Raul Guiu
Raul Guiu

Reputation: 2404

The timezoneOffset is in minutes, you shoul ddo:

userTimeChoiceConvertedToUtc  = new Date (userPickedTime.getTime() + 
(userPickedTime.getTimezoneOffset() * 60000));

Upvotes: 1

Related Questions