Reputation: 1741
Here's my date string:
2022-07-20T11:34:39
And here's my code to get GMT:
new Date('2022-07-20T11:34:39').toGMTString()
// prints this
// Wed, 20 Jul 2022 07:04:39 GMT
However, I only need the date part. Wed, 20 Jul 2022
. I know I can use substring or regex or split etc. to extract it.
Is there any solution to achieve this without those tricks?
Upvotes: 0
Views: 386
Reputation: 2358
const dateFormatter = new Intl.DateTimeFormat('en-GB', {
year: 'numeric',
month: 'long',
day: 'numeric',
weekday: 'short'
})
const dateFormatter2 = new Intl.DateTimeFormat('en-GB', {
dateStyle: 'full'
})
const cdate = new Date()
console.log(dateFormatter.format(cdate));
console.log(dateFormatter2.format(cdate));
Upvotes: 1
Reputation: 5853
First of all toGMTString() is depracated:
Note: toGMTString() is deprecated and should no longer be used. It remains implemented only for backward compatibility; please use toUTCString() instead.
You are looking for toDateString():
const res = new Date('2022-07-20T11:34:39').toDateString()
console.log(res)
Or more configurable toLocaleDateString():
const options = {
weekday: 'short',
year: 'numeric',
month: 'short',
day: 'numeric'
};
const res = new Date('2022-07-20T11:34:39').toLocaleDateString('en-US', options)
console.log(res)
Upvotes: 3