xander
xander

Reputation: 381

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

Upvotes: 0

Views: 85

Answers (2)

Rohìt Jíndal
Rohìt Jíndal

Reputation: 27232

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);

Updating the answer as per the comment by author (In Vue.js format) :

new Vue({
  el: '#app',
  data: {
    product: {
        time: '2022-09-06T07:11:59.002Z'
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <p>{{ product.time.replace(product.time.match(/(\.).*/g)[0], '').replace('T', ' ') }}</p>
</div>

Upvotes: 1

Taha Azzabi
Taha Azzabi

Reputation: 2570

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

Upvotes: 0

Related Questions