How to calculate Mother’s Day in JavaScript

Josh Sherman
1 min read
Software Development JavaScript

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!

Join the Conversation

Good stuff? Want more?

Weekly emails about technology, development, and sometimes sauerkraut.

100% Fresh, Grade A Content, Never Spam.

About Josh

Husband. Father. Pug dad. Musician. Founder of Holiday API, Head of Engineering and Emoji Specialist at Mailshake, and author of the best damn Lorem Ipsum Library for PHP.

Currently Reading

Parasie Eve

Previous Reads

Buy Me a Coffee Become a Sponsor

Related Articles