Get next day of week in JavaScript – strtotime equivalent

Sometimes it is necessary for coders to get the next monday, tuesday, wednesday orany other day of the week.

As a PHP coder, I can simply use the strtotime()  function in PHP to get a timestamp for, let’s say,  next wednesday. The code would look like this

echo strtotime("now"), "\n";
echo strtotime("10 September 2000"), "\n";
echo strtotime("+1 day"), "\n";
echo strtotime("+1 week"), "\n";
echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n";
echo strtotime("next Thursday"), "\n";
echo strtotime("last Monday"), "\n";

Now, there is no JavaScript equivalent to PHPs strtotime  function. So I needed to find one or write my own function to get the Date  object of the next occurence of a given day of week.

Here is something I have found on stackoverflow that pretty much did what I needed. I tweaked it a bit to be able to also set hours, minutes and seconds.

This is the original code from stackoverflow.

function getNextDayOfWeek(date, dayOfWeek) {
    // Code to check that date and dayOfWeek are valid left as an exercise ;)

    var resultDate = new Date(date.getTime());

    resultDate.setDate(date.getDate() + (7 + dayOfWeek - date.getDay()) % 7);

    return resultDate;
}

And here is my tweaked version of this code. I needed this for the Apache Cordova Telerik LocalNotifications Plugin call, that’s why it is called getDateForNotification  :)

var app = {
     getDateForNotification : function(dayOfWeek, hours, minutes, seconds){
        var d = new Date();
        var returnDate = new Date();
        returnDate.setDate(d.getDate() + (dayOfWeek+(7-d.getDay())) % 7);
        returnDate.setHours(hours);
        returnDate.setMinutes(minutes);
        returnDate.setSeconds(seconds);
        return returnDate;
    }
};

So when you need the Date  object for the next Thursday at 15:03:22, you simply call the method as follows:

var myDate = app.getDateForNotification(4, 15, 03, 22)
alert(myDate)

The first parameter is the day of the week.

1 = Monday
2 = Tuesday
3 = Wednesday
4 = Thursday
5 = Friday
6 = Saturday
7 = Sunday

The following parameters are hours , minutes  and seconds

The above method call returns (at the time fo writing this)

Thu Jun 01 2017 15:03:22 GMT+0200 (CEST)

As you can see, this is not really an equivalent to PHPs strtotime()  function, as it only gets the next occurence for a given day of week. It will not get you “monday in two weeks” or stuff like that.

But if you need to get the Date  object for the next Saturday at 23:45:22, this method is for you :)

I hope this helps some people out there. If you think you have good additions to be added to this code, let me know in the comments and I will add them.

Veröffentlicht von

Christian Hänsel

Web-Entwickler, Experte für technisches SEO. ich liebe meinen Job, meine Familie und den Libanon. Fragen? Mail: chris@haensel.pro.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.