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?
>Solution :
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