One of my favorite features of PHP is the strtotime function. It takes a string and gives you the equivalent time in return.

Unfortunately, there’s not something similar for other languages. There are other ATTEMPTS to duplicate the function, but nothing I’ve ran into that works quite as well.

With that, sometimes you need to get your hands dirty and write the logic out. Mother’s day falls on the second Sunday of May here in the states.

To start, you need to determine what day of the week May 1st falls on:

var mayFirst = new Date('2017-05-01');
var dayOfWeek = mayFirst.getUTCDay();

Now that we know what day of the week it falls on, we can calculate the first Sunday of the month:

var firstSunday;

if (dayOfWeek === 0) {
  firstSunday = mayFirst;
} else {
  firstSunday = new Date();
  firstSunday.setDate(1 + (7 - dayOfWeek));

So we know what the first Sunday date is, all we need to do is add another 7 days to that:

var mothersDay = new Date(firstSunday);
mothersDay.setDate(firstSunday.getUTCDate() + 7);
mothersDay = new Date(mothersDay);

Now you have a Date object that is set to the second Sunday of May!

Did you enjoy this post?

Cool if I slip into your inbox with more?
Full posts, 1-2 times per week: