rgaedrz
rgaedrz

Reputation: 43

How to get a timestamp in UTC but without the time?

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.

Upvotes: 0

Views: 1160

Answers (2)

Sunny
Sunny

Reputation: 615

By using substring:

let d = new Date();
d.toString().substring(0,15)

Upvotes: 0

Daisho Arch
Daisho Arch

Reputation: 519

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

Upvotes: 1

Related Questions