1

i am a newcomer in javascript and have a question regarding unix timestamps in javascript.

suppose the date is "2009/11/23" or "1890-03-12" or "1890-03-12 12:23:00" or current date through "new Date()".

How to convert these dates to unix timestamps or epoch timestamps with detailed explanation ?

I am confused and have no idea about how to convert it into epoch timestamps in javascript ?

arjun sah
  • 407
  • 3
  • 11
  • 1
    Parse the string to its components for year, month and day then use the Date constructor and *getTime* method: `new Date(year, month - 1, day).getTime()`. That will be milliseconds, divide by 1,000 to get seconds. Note months are zero indexed so subtract 1 from the calendar month number. Don't use the built–in parser. – RobG Aug 23 '20 at 22:17

2 Answers2

0

for unix timestamp

 new Date('2020.08.24').getTime() / 1000

For more information go through Documentation

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

  • this should be the accepted answer, mostly because it's correct, but also because it's the simplest way to get the job done, which is quite important for a new user. – daf Aug 24 '20 at 12:56
  • Thanks @daf !! ..for accepting answer. – harshadkapei Aug 24 '20 at 13:27
  • @harshadkapei but when i checked your answer in browser console but with different date formats such as "new Date('2020/08/24').getTime() / 1000" or "new Date('2020-08-24').getTime() / 1000" i get a different answer. which one i shall consider correct. Please suggest. – arjun sah Aug 24 '20 at 15:47
  • Looks like the second one is including the timezone offset in the answer, which is probably why RobG warned about it. So here's the big hitter: if you're not just don't this for learning, check out a library like moment or luxon. If you're just learning at this stage, you've learned an important lesson early on: JavaScript dates suck. – daf Aug 24 '20 at 16:23
  • I have compared '2020.08.24' format date value to '2020/08/24' and '2020-08-24' format date respectively in browser and got '2020/08/24' this format value same . So I suggested you to go througth '2020/08/24' this format date. – harshadkapei Aug 24 '20 at 19:52
  • Using the built–in parser is a bad idea, `new Date('2020.08.24')` produces an invalid date in at least one current implementation as does `new Date('1890-03-12 12:23:00')`. See [*MDN: Date.parse*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse). – RobG Aug 24 '20 at 20:28
0

You're probably looking for Date.parse (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse) which parses a date string to return an epoch timestamp.

daf
  • 1,289
  • 11
  • 16
  • Just posting a reference to another site is not an answer, it might be a helpful comment. Using the built–in parser is not encouraged. – RobG Aug 23 '20 at 21:45
  • it's the simplest answer for a person new to JavaScript. Introducing something heavy like Moment or Lumen is crazy when someone is just trying to learn. In addition, copy-pasting MDN documentation wouldn't have been any clearer. – daf Aug 24 '20 at 12:51