简体   繁体   中英

How can I format time

I get this time stamp from the database :

2022-09-06T07:11:59.002Z

How can I format it to something like this?

2022-09-06 07:11:59

You can achieve this with the help of RegEx by using String.replace() method along with String.match()

Live Demo :

 const str = '2022-09-06T07:11:59.002Z'; const res = str.replace(str.match(/(\.).*/g)[0], '').replace('T', ' '); console.log(res);

This not related to VueJS, it's a pure JavaScript question:

Ill suggest to transform the string date to a Date Object, and then you can manipulate it easily:

 const date = new Date('2022-09-06T07:11:59.002Z'); console.log(date.toISOString().slice(0, 19).replace('T', ' ')); console.log(date.toLocaleDateString('fr-FR', { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }));

More information about Date object

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM