0

I have an AJAX request that returns an array, in this array I capture the created_at from my database and it comes in the following format:

Code:

success: function(response){
     let data = response;
     date = data[0][`created_at`];

     console.log(date);
}

log:

2022-08-25T18:44:48.000000Z

I need to split this string into two variables (date and time), what is the best way?

MrCodingB
  • 2,284
  • 1
  • 9
  • 22

2 Answers2

3

you can do it like this,

success: function(response){
     let data = response;
     date = data[0][`created_at`];
     
     // date.split('T') - this will create an array like ['2022-08-25', '18:44:48.000000Z']
     // const [date, time] - this is Array destructuring in action.

     const [date, time] = date.split('T') 
     
     // date - '2022-08-25'
     // time - '18:44:48.000000Z'

     console.log(date, time)
}

checkout this: What does this format mean T00:00:00.000Z?

also checkout: Destructuring assignment

Vinod Liyanage
  • 945
  • 4
  • 13
0
Let date = new Date(data[0][`created_at`]);
Console.log(date.format('dd/mm/yy'))

Try to install moment.js