How do I convert ISO 8601 date (ex. 20140107) to Unix timestamp (ex. 1389120125) using javascript?
If you're using a browser with ECMAscript 5 support, Date.parse() accepts an ISO-8601 datestring and returns an epoch value in milliseconds, so just divide that by 1000 and you're done.
Contrary to what you state, your input string doesn't conform to the ISO-8601 subset defined in ECMAscript because it's lacking the dashes between the individual fields. As far as I know, the dashes are mandatory for EMCAscript (even though ISO 8601 itself allows the dashless, or basic, format). So maybe you'll have to do some string parsing and use one of Date's constructors and its getTime() method to obtain the same
new Date(year, month [, day, hour, minute, second, millisecond]);
If you want to remain compatible with older browsers but still use Date.parse, you could consider including this shim
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With