-2

I need to create a Timestamp like below. I have tried lots of ways , but couldn't get this format.

How can I create a current time and date in this Timestamp format in JavaScript ??

2018-08-20T04:19:50.670Z

Amithash
  • 269
  • 2
  • 4
  • 22
  • 1
    Possible duplicate of [How do you get a timestamp in JavaScript?](https://stackoverflow.com/questions/221294/how-do-you-get-a-timestamp-in-javascript) – Anshuman Aug 20 '18 at 06:07

2 Answers2

2

Try this

console.log(new Date().toISOString());
saravanan mp
  • 745
  • 3
  • 16
  • 34
  • 1
    Don't even need the `toISOString()`. – jhpratt Aug 20 '18 at 06:12
  • @jhpratt Without toISOString() whatever format we expect, that won't come. – saravanan mp Aug 20 '18 at 06:16
  • Have you tried it? I did and you get the expected format. – jhpratt Aug 20 '18 at 06:16
  • @jhpratt for console.log(new Date()); Output : Mon Aug 20 2018 11:47:26 GMT+0530 (India Standard Time) console.log(new Date().toISOString()); Output : 2018-08-20T06:17:29.129Z – saravanan mp Aug 20 '18 at 06:18
  • @saravananmp - When I console.log(new Date().toISOString()); this , I got the Output : 2018-08-20T06:17:29.129Z. But , when I did like this - var timestampama = new Date().toISOString(); console.log("Timestamp - " +timestampama); , it always gives me only 6. How can I Fix this ?? – Amithash Aug 20 '18 at 06:32
1

You can create a Timestamp like 2018-08-20T04:19:50.670Z using

console.log(new Date().toISOString());

Or,

console.log(new Date());

But note that in chrome

console.log(new Date().toISOString());

will return timestamp in this format:

`2018-08-20T06:19:24.386Z`

but

console.log(new Date());

will return timestamp in this format: Mon Aug 20 2018 11:50:23 GMT+0530 (India Standard Time)

But in Mozilla, both yeilds same result.

If you use .toISOString() with new Date(), then it will return a string of the timestamp, otherwise it will return a Date object. This is the only difference in using .toISOString(). So, its depends on you what do you want???

Anshuman
  • 758
  • 7
  • 23
  • Interesting that Chrome does that, because when I tried it in Node (which also uses V8), it did the same as Firefox. – jhpratt Aug 20 '18 at 06:24
  • @jhpratt, does `console.log(new Date().toISOString());` and `console.log(new Date());` give the same result in `node`. – Anshuman Aug 20 '18 at 06:27
  • Yes, at least in node 8.11.2 – jhpratt Aug 20 '18 at 06:27
  • @jhpratt, yes, in node both yeilds same format of timestamp. – Anshuman Aug 20 '18 at 06:29
  • I checked in `Node 8.11.4` – Anshuman Aug 20 '18 at 06:29
  • I was more intrigued as to _why_ there's a difference, when both Chrome and Node use V8. – jhpratt Aug 20 '18 at 06:29
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/178322/discussion-between-inullpointer-and-jhpratt). – Anshuman Aug 20 '18 at 06:30
  • @iNullPointer - When I console.log(new Date().toISOString()); this , I got the Output : 2018-08-20T06:17:29.129Z. But , when I did like this - var timestampama = new Date().toISOString(); console.log("Timestamp - " +timestampama); , it always gives me only 6. How can I Fix this ?? – Amithash Aug 20 '18 at 06:50