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

How to convert date into unix time?

If I have a date that looks like this?

Sun, Nov 14, 2021 04:52:00PM

How can I convert this into unix time?

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

I did new Date(‘Sun, Nov 14, 2021 04:52:00PM’)
and .getTime() to it however i get returned NaN?

Is there a way of doing this moment or js?

>Solution :

With momentjs you can use the ddd, MMM DD, YYYY HH:mm:ss pattern:

const d = "Sun, Nov 14, 2021 04:52:00PM"

const parsed = moment('Sun, Nov 14, 2021 04:52:00PM',
                      'ddd, MMM DD, YYYY HH:mm:ss').toDate()
console.log(parsed)
console.log('unix:', parsed.getTime())
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
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