Member-only story

Altering content based on the date using JavaScript

Erika in Glasses
2 min readJun 18, 2020

--

When you work for a company that sells things, you’re going to end up building landing pages that have sale prices or bonus points that are based on a date range.

At first, we were coding and scheduling separate code blocks based on the dates. But! I was able to figure out javascript to grab the date and show the sale item or not.

Here is an example of grabbing the date and the month, and then showing a different headline if the current date is between the 12 and the 17th of February.

Let's work through this code:

I’ve included screenshots of the results in the console based on today

1. We need to store the date in a variable:

var currentDate = new Date();

2. Then we need to split this date into two variables; day and the month. I used .getDate() and .getMonth() to pull this information from the variable that I stored the current date and time in. Just a note getDay() results in the day of the week, not the day of the month.

  var currentDay = currentDate.getDate();

--

--

Erika in Glasses
Erika in Glasses

Written by Erika in Glasses

Web developer. Twitch Affiliate (erikainglasses). Youtuber. Olympic lifter. I write about code and streamer.

No responses yet