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

Create UTC Timestamp in Javascript - Stack Overflow

programmeradmin5浏览0评论

I am trying to send UTC time-stamp to rest service from my javascript client. i was not able to create time-stamp like "2013-08-30T19:52:28.226Z" using javascript.

var rawDate = date.getUTCDate().toString();

i see this example but not helpful for me. utc-time-stame-javascript

I am trying to send UTC time-stamp to rest service from my javascript client. i was not able to create time-stamp like "2013-08-30T19:52:28.226Z" using javascript.

var rawDate = date.getUTCDate().toString();

i see this example but not helpful for me. utc-time-stame-javascript

Share Improve this question edited May 23, 2017 at 10:29 CommunityBot 11 silver badge asked Aug 31, 2013 at 8:54 Shashank ShuklaShashank Shukla 1,1461 gold badge17 silver badges23 bronze badges 4
  • Did you mean new Date()? It returns the (complete) current date, with all the properties of the Date object, in your case (new Date()).toJSON() should work – Niccolò Campolungo Commented Aug 31, 2013 at 9:05
  • As my question i want 2013-08-31T9:4:51.859Z in js – Shashank Shukla Commented Aug 31, 2013 at 9:06
  • new Date().toJSON() return 2013-08-31T09:04:51.863Z – Shashank Shukla Commented Aug 31, 2013 at 9:08
  • possible duplicate of How do you convert a JavaScript date to UTC? – vascowhite Commented Oct 6, 2013 at 8:15
Add a comment  | 

6 Answers 6

Reset to default 13

You can use date.toJSON().

new Date().toJSON()
"2013-08-31T09:05:07.740Z"

See MDN or MSDN

1) Get the date.

var now = new Date();

2) Convert to UTC format like below, for reference.

var now_utc = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), 
                  now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds());

3) Using toJSON, get the format.

now_utc.toJSON()

Finally,

var now = new Date();
var now_utc = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(),  now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds());
alert(now_utc.toJSON());

Check this JSFiddle

function getUTCISODateString(d){
     function pad(n){return n<10 ? '0'+n : n};
     function threePad(n){return n<10 ? '00'+n : (n < 100 ? '0' + n : n)};
     return d.getUTCFullYear()+'-'
          + pad(d.getUTCMonth()+1)+'-'
          + pad(d.getUTCDate())+'T'
          + pad(d.getUTCHours())+':'
          + pad(d.getUTCMinutes())+':'
          + pad(d.getUTCSeconds())+ '.'
                  + threePad(d.getUTCSeconds()) + 'Z';
}

Not tested :

This library can do it for you. Not that big either http://momentjs.com

moment().toISOString() 
// 2013-02-04T22:44:30.652Z

I would suggest extending the Date() object and building the string yourself, moment does it for you but I'm not sure it's in the exact format you need. Just wrote this up quickly, but it should be a decent starter boiler plate.

Date.prototype.toLongUTCString = function () {
   var self = this;
   return self.getUTCFullYear() + '-' + (self.getUTCMonth() < 10 ? '0' : '') + 
          (self.getUTCMonth() +1)+ '-' + (self.getUTCDate() < 10 ? '0' : '') + 
          self.getUTCDate() + 'T' + self.getUTCHours() + ':' + self.getUTCMinutes() + 
          ':' + self.getUTCSeconds() + '.' + self.getUTCMilliseconds() + 'Z';
   };

See more:

http://jsfiddle.net/4Kczy/

/edit: no one bothered to ask what browsers need to be supported (cough, IE).

const date = new Date()
const timestamp = date.toJSON()
const humanReadableDate = date.toLocaleString()
console.log(date)              // Fri Mar 18 2022 21:22:57 GMT-0700 (Pacific Daylight Time)
console.log(timestamp)         // 2022-03-19T04:22:57.983Z
console.log(humanReadableDate) // 3/18/2022, 9:22:57 PM
发布评论

评论列表(0)

  1. 暂无评论