Javascript equivalent php strtotime ()?

In PHP, you can easily convert a text-based textual description of the date and time to the corresponding date using strtotime() .

Is there something similar in javascript?

+77
javascript date
Oct 29 '10 at 0:40
source share
8 answers

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

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

It works?

+48
Oct 29 '10 at 0:48
source share
— -

Yes it is. And it is supported in all major browsers:

 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 which valid formats JavaScript can handle.

+61
Feb 05 '12 at 18:51
source share

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

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

Update: this function according to version 1.0.2 cannot handle this case: '2007:07:20 20:52:45' (note : separator for year and month)

Update 2018:

Now it is available as an npm module! Just npm install locutus and then to your source:

 var strtotime = require('locutus/php/datetime/strtotime'); 
+31
Jul 12 '13 at 2:08 on
source share

I am jealous of strtotime () in php, but I do mine in javascript using the moment. Not as sweet as from php, but the trick is also neat.

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

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

 // last day of previous month var yesterMonthLastDay = moment(yesterMonthLastDay).subtract(1,'months').endOf('month').toDate(); 
+4
Jan 09 '16 at 20:43
source share

There are several modules that provide similar behavior, but not like PHP strtotime. Among the few alternatives, I found date-util , which gives the best results.

0
Jun 14 '16 at 23:47
source share

Maybe you can use an example function, for example:

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

''

0
May 24 '19 at
source share

Browser support for line parsing is incompatible . Since there are no specifications for 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 
-one
May 8 '16 at 20:49
source share

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



All Articles