最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Create UTC date in JavaScript, add a day, get unix timestamp - Stack Overflow

programmeradmin0浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 5

If 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

发布评论

评论列表(0)

  1. 暂无评论