7

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

Community
  • 1
  • 1
Shashank Shukla
  • 1,146
  • 1
  • 17
  • 23

6 Answers6

13

You can use date.toJSON().

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

See MDN or MSDN

Benoit Blanchon
  • 13,364
  • 4
  • 73
  • 81
  • see my desire result 2013-08-31T9:9:55.893Z – Shashank Shukla Aug 31 '13 at 09:11
  • Oh ow. A W3 Schools link? Here's a better resource discussing the function: [MDN Date#toJSON](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toJSON?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FGlobal_Objects%2FDate%2FtoJSON) – Brandon Buck Aug 31 '13 at 09:11
  • 1
    What's the issue with W3 Schools ? – Benoit Blanchon Aug 31 '13 at 09:13
  • @shashank-shukla I don't get it, what's different ? – Benoit Blanchon Aug 31 '13 at 09:16
  • There's a site, called W3 Fools, it explains it in more depth. They are not an API resource or designed to be referenced, they present themselves as a "school" of sorts but contain (and teach) incorrect information, outdated information, etc. All in all, if you're expressing something that requires an API link use MDN (for Javascript) or you can get there, and several others at [Dochub](http://www.dochub.io). – Brandon Buck Aug 31 '13 at 09:18
  • Anywho, aside from the W3 link you're answer is the most efficient and simplest solution (other than just stringifying an object containing a Date object and having this done automatically) so I'll give you a +1 – Brandon Buck Aug 31 '13 at 09:20
  • @Benoit Blanchon thanks this was my browser cache ...thanks a lot. – Shashank Shukla Aug 31 '13 at 09:25
  • @ShashankShukla Does `.toJSON()` convert date into UTC? I can't anything in docs. – Praveen Aug 31 '13 at 09:34
  • @user1671639 as i compare with utc time and it's shows correct result. – Shashank Shukla Aug 31 '13 at 10:21
  • @BenoitBlanchon is there any difference between new Date().toISOString() and new Date().toJSON()? – Artur Carvalho Sep 10 '18 at 14:39
  • 1
    @ArturCarvalho, according to [this answer](https://stackoverflow.com/a/16198620/1164966), they are identical, except that `toISOString()` requires IE9+ – Benoit Blanchon Sep 11 '18 at 08:22
1

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

Community
  • 1
  • 1
Praveen
  • 55,303
  • 33
  • 133
  • 164
1
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 :

sabithpocker
  • 15,274
  • 1
  • 42
  • 75
0

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

moment().toISOString() 
// 2013-02-04T22:44:30.652Z
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
  • The format is off. His requirements call for milliseconds appended before timezone. – Brandon Buck Aug 31 '13 at 09:15
  • The inclusion of `moment` here (while a great library) is unnecessary unless more date processing needs to be done. As answered by Benoit Blanchon this can be achieved natively with `new Date().toJSON()` but even simpler with `JSON.stringify({post_date: new Date()});` in which case it's called automatically. I should have mentioned this earlier, but good on the update! – Brandon Buck Aug 31 '13 at 09:22
  • @izuriel I did notice that after i read your other comment `JSON.stringify` will do it – Ruan Mendes Aug 31 '13 at 09:24
0

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).

konp
  • 33
  • 4
  • This functionality can be added to the `Date` class as well as full `JSON` parsing/string building with the [json2](https://github.com/douglascrockford/JSON-js) library. A common rule of thumb, by the way, is avoid extending core classes as much as possible. – Brandon Buck Aug 31 '13 at 09:34
  • @izuriel - it depends what you're extending - e.g. this function will never be implemented in the core spec (unlike map/filter/reduce might be implemented at some point in the future in IE). As for the json2 lib - you don't always need a chainsaw to cut down a branch. Cheers! – konp Aug 31 '13 at 09:51
0
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
jasonleonhard
  • 12,047
  • 89
  • 66