Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Extract datetime from string with javascript

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading