0

I need to generate a date in UTC format, but without the time part, that is, instead of getting:

Wed, 19 Jan 2021 19:27:00 GMT

I need only:

Wed, 19 Jan 2021

I know that there is a "long" way to do it by formatting the string myself using methods one instance of Date() and concatenating them. But I would like to know if there is a shorter way to do it.

By the way, I can only use Javascript vanilla.

rgaedrz
  • 43
  • 7
  • UTC is not a format, it's a time standard. If what you want is a UTC date, consider `new Date().toLocaleDateString('default',{timeZone:'UTC'})`. Formatting of dates has been [exhaustively covered in other questions](https://stackoverflow.com/search?q=%5Bjavascript%5D+how+to+format+a+date), e.g. [*How to format a JavaScript date*](https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date?r=SearchResults&s=1|1682.8208). – RobG Jan 20 '21 at 21:40

2 Answers2

1

I don't know what you mean by long but the code for extracting only the part you're looking for is pretty short.

'Wed, 19 Jan 2021 19:27:00 GMT'.split(' ').slice(0, -2)

Edit: If you really dislike this code I just found out that Date.toDateString() exists

Daisho Arch
  • 143
  • 11
0

By using substring:

let d = new Date();
d.toString().substring(0,15)
Sunny
  • 559
  • 4
  • 17