Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript equivalent of php's strtotime()?

In PHP, you can easily convert an English textual datetime description into a proper date with strtotime().

Is there anything similar in Javascript?

like image 280
VJS Avatar asked Oct 29 '10 00:10

VJS


People also ask

What does Strtotime return?

Return Values PHP strtotime() function returns a timestamp value for the given date string. Incase of failure, this function returns the boolean value false.

Which is a valid Strtotime () function in PHP?

The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT). Note: If the year is specified in a two-digit format, values between 0-69 are mapped to 2000-2069 and values between 70-100 are mapped to 1970-2000.


2 Answers

There is not. The closest built-in option is Date.parse(), which parses a very limited subset of what strtotime() can:

var ts = Date.parse("2010-10-29"); 

It's worth noting that this function returns milliseconds instead of seconds, so you need to divide the result by 1000 to get an equivalent value to PHP's function.

like image 113
Arnaldo Avatar answered Oct 18 '22 22:10

Arnaldo


I found this article and tried the tutorial. Basically, you can use the date constructor to parse a date, then write get the seconds from the getTime() method

var d=new Date("October 13, 1975 11:13:00"); document.write(d.getTime() + " milliseconds since 1970/01/01"); 

Does this work?

like image 27
usr-local-ΕΨΗΕΛΩΝ Avatar answered Oct 18 '22 23:10

usr-local-ΕΨΗΕΛΩΝ