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

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 Commented Aug 31, 2013 at 9:05
  • As my question i want 2013-08-31T9:4:51.859Z in js Commented Aug 31, 2013 at 9:06
  • new Date().toJSON() return 2013-08-31T09:04:51.863Z Commented Aug 31, 2013 at 9:08
  • possible duplicate of How do you convert a JavaScript date to UTC? Commented Oct 6, 2013 at 8:15

6 Answers 6

13

You can use date.toJSON().

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

See MDN or MSDN

Sign up to request clarification or add additional context in comments.

11 Comments

see my desire result 2013-08-31T9:9:55.893Z
Oh ow. A W3 Schools link? Here's a better resource discussing the function: MDN Date#toJSON
What's the issue with W3 Schools ?
@shashank-shukla I don't get it, what's different ?
@ArturCarvalho, according to this answer, they are identical, except that toISOString() requires IE9+
|
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

1 Comment

Not utc time.. 2013-08-31T03:44:49.000Z
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 :

2 Comments

new Date().toJSON()
If IE7 were a concern for support then I'd bring in a library like json2 to polyfill that functionality onto Date as well as JSON.stringify and JSON.parse.
0

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

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

3 Comments

The format is off. His requirements call for milliseconds appended before timezone.
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!
@izuriel I did notice that after i read your other comment JSON.stringify will do it
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).

2 Comments

This functionality can be added to the Date class as well as full JSON parsing/string building with the json2 library. A common rule of thumb, by the way, is avoid extending core classes as much as possible.
@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!
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.