Aquí está mi cadena de fecha:
2022-07-20T11:34:39Y aquí está mi código para obtener GMT:
new Date('2022-07-20T11:34:39').toGMTString() // prints this // Wed, 20 Jul 2022 07:04:39 GMT Sin embargo, solo necesito la parte de la fecha. Wed, 20 Jul 2022 . Sé que puedo usar substring o regex o split, etc. para extraerlo.
¿Hay alguna solución para lograr esto sin esos trucos?
En primer lugar , toGMTString() está en desuso:
Nota: toGMTString() está en desuso y ya no debe usarse. Permanece implementado solo por compatibilidad con versiones anteriores; utilice toUTCString() en su lugar.
Está buscando toDateString() :
const res = new Date('2022-07-20T11:34:39').toDateString() console.log(res)O más 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)usar Intl.DateTimeFormat
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));