0

How can I parse a date such as the following and convert it to a Unix timestamp using JavaScript?

Sat Mar 29 2014 16:10:00 GMT+0800 (Taipei Standard Time)

Thanks.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Cris
  • 437
  • 2
  • 5
  • 15
  • new Date("Sat Mar 29 2014 16:10:00 GMT+0800 (Taipei Standard Time)").getTime()/1000; with a result of 1396195200 which is not a correct date. – Cris Mar 31 '14 at 06:25
  • possible duplicate of [Converting Date and Time To Unix Timestamp](http://stackoverflow.com/questions/1791895/converting-date-and-time-to-unix-timestamp) – fny Mar 31 '14 at 06:33

1 Answers1

-3

you just need a good date-parsing function, I would look at date.js . It will take just about any date string you can throw at it, and return you a JavaScript Date object. Once you have a Date object, you can call its getTime() method, which will give you milliseconds since January 1, 1970. Just divide that result by 1000 to get the unix timestamp value. In code, just include date.js, then: var unixtime = Date.parse("24-Nov-2009 17:57:35") .getTime()/1000

Get date.js from http://www.datejs.com/

More here: https://stackoverflow.com/a/1792009/390897

Community
  • 1
  • 1
BlackPearl
  • 2,532
  • 3
  • 33
  • 49
  • Copied and pasted from [http://stackoverflow.com/a/1792009/390897](http://stackoverflow.com/a/1792009/390897)... – fny Mar 31 '14 at 06:32
  • Yes? Cause I attempted to help and you want the vote and you didn't see the reference link? Pathetic – BlackPearl Mar 31 '14 at 06:35
  • using datejs, i recieved error: TypeError: Date.parse(...) is null var unixtime = Date.parse(ev["start_date"]).getTime()/1000 which ev["start_date"] is equal to "Sat Mar 29 2014 16:10:00 GMT+0800 (Taipei Standard Time)" – Cris Mar 31 '14 at 06:46
  • DateJS will not understand the last portion of your string "(Taipei Standard Time)", so you'll need to remove that with some regex or use a different library (e.g. moment.js) that can handle such strings. – fny Mar 31 '14 at 06:52