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?

6
  • 1
    how do you want it split Commented Aug 26, 2022 at 18:16
  • What is the format you want the date to be? Commented Aug 26, 2022 at 18:17
  • I need the date and time separated into two different strings (date = XX-XX-XXXX and time = XX:XX:XX) Commented Aug 26, 2022 at 18:19
  • .split('T') ? Commented Aug 26, 2022 at 18:21
  • Great but how do I remove the .000000Z from the end of the hour? Commented Aug 26, 2022 at 18:24

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

0
Let date = new Date(data[0][`created_at`]);
Console.log(date.format('dd/mm/yy'))

Try to install moment.js

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.