How do I get a UTC Timestamp in JavaScript?

While writing a web application, it makes sense to store (server side) all datetimes in the DB as UTC timestamps.

I was astonished when I noticed that you couldn't natively do much in terms of Timezone manipulation in JavaScript.

I extended the Date object a little. Does this function make sense? Basically, every time I send anything to the server, it's going to be a timestamp formatted with this function...

Can you see any major problems here? Or maybe a solution from a different angle?

Date.prototype.getUTCTime = function(){ 
  return new Date(
    this.getUTCFullYear(),
    this.getUTCMonth(),
    this.getUTCDate(),
    this.getUTCHours(),
    this.getUTCMinutes(), 
    this.getUTCSeconds()
  ).getTime(); 
}

It just seems a little convoluted to me. And I am not so sure about performance either.


  • Dates constructed that way use the local timezone, making the constructed date incorrect. To set the timezone of a certain date object is to construct it from a date string that includes the timezone. (I had problems getting that to work in an older Android browser.)

  • Note that getTime() returns milliseconds, not plain seconds.

  • For a UTC/Unix timestamp, the following should suffice:

    Math.floor((new Date()).getTime() / 1000)
    

    It will factor the current timezone offset into the result. For a string representation, David Ellis' answer works.

    To clarify:

    new Date(Y, M, D, h, m, s)
    

    That input is treated as local time. If UTC time is passed in, the results will differ. Observe (I'm in GMT +02:00 right now, and it's 07:50):

    > var d1 = new Date();
    > d1.toUTCString();
    "Sun, 18 Mar 2012 05:50:34 GMT" // two hours less than my local time
    > Math.floor(d1.getTime()/ 1000)
    1332049834 
    
    > var d2 = new Date( d1.getUTCFullYear(), d1.getUTCMonth(), d1.getUTCDate(), d1.getUTCHours(), d1.getUTCMinutes(), d1.getUTCSeconds() );
    > d2.toUTCString();
    "Sun, 18 Mar 2012 03:50:34 GMT" // four hours less than my local time, and two hours less than the original time - because my GMT+2 input was interpreted as GMT+0!
    > Math.floor(d2.getTime()/ 1000)
    1332042634
    

    Also note that getUTCDate() cannot be substituted for getUTCDay() . This is because getUTCDate() returns the day of the month; whereas, getUTCDay() returns the day of the week.


    你也可以使用getTimezoneOffset和getTime,

    x = new Date()
    var UTCseconds = (x.getTime() + x.getTimezoneOffset()*60*1000)/1000;
    

    Yeah, you don't need to do that much; assuming I understand you correctly, you just want the toUTCString method.

    var UTCstring = (new Date()).toUTCString();
    

    However, remember that the date values you get are based on the clock of the client's machine, not your server. If you need precise values for these dates (for instance, to properly order when this or that action was done relative to another user's action), you can't rely on the client-side date library, anyways, and you need to calculate the dates server-side based on when the client contacted you.

    Remember as well that basically all of your client-side code can be modified by the client and malicious values returned instead, you can only guarantee the code on the server-side -- treat the client-side like a possible attacker.

    链接地址: http://www.djcxy.com/p/77468.html

    上一篇: 如何设置小时,分钟,秒到格林尼治标准时间的日期

    下一篇: 如何在JavaScript中获得UTC时间戳?