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();
to_date.setUTCHours(15);
to_date.setUTCMinutes(25);
var to_utc = new Date(to_date.getUTCFullYear(),
to_date.getUTCMonth(),
to_date.getUTCDate(),
to_date.getUTCHours(),
to_date.getUTCMinutes(),
to_date.getUTCSeconds());
var to_unix = Math.round(to_utc.getTime() / 1000);
console.log(to_unix);
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
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();
to_date.setUTCHours(15);
to_date.setUTCMinutes(25);
var to_utc = new Date(to_date.getUTCFullYear(),
to_date.getUTCMonth(),
to_date.getUTCDate(),
to_date.getUTCHours(),
to_date.getUTCMinutes(),
to_date.getUTCSeconds());
var to_unix = Math.round(to_utc.getTime() / 1000);
console.log(to_unix);
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 asked Sep 13, 2012 at 19:03 dzmdzm 23.6k51 gold badges152 silver badges229 bronze badges 1- this link may help you: stackoverflow./questions/221294/…. – Anoop Commented Sep 13, 2012 at 19:45
3 Answers
Reset to default 5If you're tinkering with an existing date object:
var d = new Date();
d.setUTCHours(15);
d.setUTCMinutes(25);
d.setUTCSeconds(0);
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()
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
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);
console.log(to_unix);
jsfiddle