Javascript equivalent of php's strtotime()?

JavascriptDate

Javascript Problem Overview


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

Is there anything similar in Javascript?

Javascript Solutions


Solution 1 - Javascript

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.

Solution 2 - Javascript

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?

Solution 3 - Javascript

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

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

[1]: http://locutus.io/php/datetime/strtotime/ "this"

> 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');

Solution 4 - Javascript

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();

Solution 5 - Javascript

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

Solution 6 - Javascript

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.

Solution 7 - Javascript

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>

Solution 8 - Javascript

Maybe you can exploit a sample function like :

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;

`

Solution 9 - Javascript

For those looking to convert the datetime to unix timestamp, you can do this using the Moment Library.

For Vue.js, you can do something like

let start = "2022/02/02 02:02:02";
return moment(start).format("x");

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVJSView Question on Stackoverflow
Solution 1 - JavascriptArnaldoView Answer on Stackoverflow
Solution 2 - Javascriptusr-local-ΕΨΗΕΛΩΝView Answer on Stackoverflow
Solution 3 - JavascriptWesty92View Answer on Stackoverflow
Solution 4 - JavascriptKhoPhiView Answer on Stackoverflow
Solution 5 - JavascriptbedrinView Answer on Stackoverflow
Solution 6 - Javascriptdoc_idView Answer on Stackoverflow
Solution 7 - JavascriptLimitless isaView Answer on Stackoverflow
Solution 8 - JavascriptAdnane ArView Answer on Stackoverflow
Solution 9 - JavascriptBlackPearlView Answer on Stackoverflow