[javascript] 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?

This question is related to javascript date

The answer is


Yes, it is. And it is supported in all major browser:

var ts = Date.parse("date string");

The only difference is that this function returns milliseconds instead of seconds, so you need to divide the result by 1000.

Check what valid formats can JavaScript parse.


Check out this implementation of PHP's strtotime() in JavaScript!

I found that it works identically to PHP for everything that I threw at it.

Update: this function as per version 1.0.2 can't handle this case: '2007:07:20 20:52:45' (Note the : separator for year and month)

Update 2018:

This is now available as an npm module! Simply npm install locutus and then in your source:

var strtotime = require('locutus/php/datetime/strtotime');

Browser support for parsing strings is inconsistent. Because there is no specification on which formats should be supported, what works in some browsers will not work in other browsers.

Try Moment.js - it provides cross-browser functionality for parsing dates:

var timestamp = moment("2013-02-08 09:30:26.123");
console.log(timestamp.milliseconds()); // return timestamp in milliseconds
console.log(timestamp.second()); // return timestamp in seconds

_x000D_
_x000D_
var strdate = new Date('Tue Feb 07 2017 12:51:48 GMT+0200 (Türkiye Standart Saati)');_x000D_
var date = moment(strdate).format('DD.MM.YYYY');_x000D_
$("#result").text(date); //07.02.2017
_x000D_
<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>_x000D_
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.js"></script>_x000D_
_x000D_
<div id="result"></div>
_x000D_
_x000D_
_x000D_


I jealous the strtotime() in php, but I do mine in javascript using moment. Not as sweet as that from php, but does the trick neatly too.

// first day of the month
var firstDayThisMonth = moment(firstDayThisMonth).startOf('month').toDate();

Go back and forth using the subtract() and add() with the endOf() and startOf():

// last day of previous month
var yesterMonthLastDay = moment(yesterMonthLastDay).subtract(1,'months').endOf('month').toDate();

There are few modules that provides similar behavior, but not exactly like PHP's strtotime. Among few alternatives I found date-util yields the best results.


Maybe you can exploit a sample function like :

_x000D_
_x000D_
function strtotime(date, addTime){_x000D_
  let generatedTime=date.getTime();_x000D_
  if(addTime.seconds) generatedTime+=1000*addTime.seconds; //check for additional seconds _x000D_
  if(addTime.minutes) generatedTime+=1000*60*addTime.minutes;//check for additional minutes _x000D_
  if(addTime.hours) generatedTime+=1000*60*60*addTime.hours;//check for additional hours _x000D_
  return new Date(generatedTime);_x000D_
}_x000D_
_x000D_
let futureDate = strtotime(new Date(), {_x000D_
    hours: 1, //Adding one hour_x000D_
    minutes: 45 //Adding fourty five minutes_x000D_
  });_x000D_
document.body.innerHTML = futureDate;
_x000D_
_x000D_
_x000D_

`