1

I am looking for a date with this format:

Fri Jul 12 13:41:12 PDT 2019

or ideally in UTC:

Fri Jul 12 10:41:12 UTC 2019

if I run this in JS: console.log(new Date()) I get something like this:

2019-07-12T20:43:04.432Z

I just find the unix date stamp more palatable for humans.

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817

3 Answers3

3

If you want to follow that exact format, something like this could work:

var dateSplit = new Date().toUTCString().replace("GMT", "UTC").replace(",", "").split(" ");

console.log(`${dateSplit[0]} ${dateSplit[2]} ${dateSplit[1]} ${dateSplit[4]} ${dateSplit[5]} ${dateSplit[3]}`);

Note: GMT can be replaced by UTC because they are essentially the same in practice

meyi
  • 7,574
  • 1
  • 15
  • 28
1

The easiest way to get something close to that would be using the toUTCString Date method. That out puts in this format: "Fri, 12 Jul 2019 20:49:50 GMT".

Another option would be using the various getters in the Date class like getFullYear to create your own format. I haven't personally done that so I can't speak on the exact way to do it. CSS-Tricks does have this article that may be of help.

W.Shields
  • 63
  • 1
  • 1
  • 10
  • can you show the exact code you used – Alexander Mills Jul 12 '19 at 21:06
  • That isn't the format produced by [*toUTCString*](http://ecma-international.org/ecma-262/10.0/#sec-date.prototype.toutcstring), the month name comes before the day number. But yes, it's close. ;-) – RobG Jul 15 '19 at 01:39
0

You can use moment even for formatting for different locales, or you can look for more lightweight alternatives on npm.

VitoMakarevich
  • 439
  • 2
  • 5
  • 1
    Something like `date-fns` would be a lot more lightweight than `moment`. `moment`'s API is *really* heavy. – Andrew Li Jul 12 '19 at 20:52
  • Saying "*use library X*" isn't an answer, it might be a useful comment (but likely not in this case). – RobG Jul 15 '19 at 01:40