Take the 2-minute tour ×

I'm trying to build dates in UTC in JavaScript, while specifying an hour and minute then getting a timestamp of it.

For example, If I have the hour of 15 and the minute of 25, I'm doing this:

var to_date = new Date();
var to_utc = new Date(to_date.getUTCFullYear(), 
var to_unix = Math.round(to_utc.getTime() / 1000);

The problem is this doesn't seem to be returning the right timestamp. It's setting the time not in UTC, but for my timezone. Any idea how to maintain this in UTC?

I also want to add a day to the time if it's past the current time. I've tried checking against the current and adding 60*60*24, but this returned minutes/hours that didn't match what I specified.

Any help would be great!

Thank you

share|improve this question
this link may help you: stackoverflow.com/questions/221294/…. –  Anoop Sep 13 '12 at 19:45

3 Answers 3

up vote 4 down vote accepted

If you're tinkering with an existing date object:

var d = new Date();

d will now represent today at 15:25 UTC.

If you're starting from scratch:

var d = new Date(Date.UTC(year, jsmonth, day, utchour, utcminute, utcsecond));

To get a unix timestamp, use getTime, which returns the number of milliseconds since epoch UTC, then divide by 1000 to convert to unix time (which is in seconds rather than milliseconds):

Math.round(d.getTime() / 1000);

Date.now() gives you the current time in UTC since epoch in milliseconds. To add a day and see if it is in the future:

d.getTime() + (1000*60*60*24) > Date.now()
share|improve this answer
How would I incorporate that, while being able to set the hour/minute of the date? –  dave Sep 13 '12 at 19:29
Sorry, I skimmed and thought you just wanted to add a day to the current time. Updated. –  josh3736 Sep 13 '12 at 19:36
That did it - thank you! –  dave Sep 13 '12 at 19:50

Try the following DEMO using your code.

To get rid of the timezone, use to_date.setHours(0,0,0,0); to set the time to 00:00:00:00

share|improve this answer
This seems to be doing the same thing, if I use epochconverter, I get GMT: Thu, 13 Sep 2012 19:25:00 GMT Your time zone: Thu Sep 13 2012 15:25:00 GMT-4, GMT should have 15:25:00 instead of my time zone. –  dave Sep 13 '12 at 19:25
Try taking a look at this post as well: stackoverflow.com/questions/439630/… –  Chase Sep 13 '12 at 19:30
Thank you! This was driving me nuts really. –  Michael D. Irizarry Jun 7 '13 at 15:17

Form link: The getTimezoneOffset() method returns the time difference between UTC time and local time, in minutes. Following should solve your problem

var to_unix = Math.round( (Date.now() + (new Date().getTimezoneOffset()) )  / 1000);


share|improve this answer
This doesn't make sense. Date.now() always deals in UTC. –  josh3736 Sep 13 '12 at 19:37

Your Answer


