JavaScript Date Comparison: Quick and Simple
Comparing dates in JavaScript is a quick, simple, no-fuss operation with one or two variations. We’ll show you how to do it, and talk a little bit about what you can do with date comparisons in JavaScript.
Interested in JavaScript programming? There are some very good online classes that will put you in the fast lane to a career in software development!
Comparing Dates – What It’s All About
What do we mean when we talk about comparing dates?
Typically, when you compare dates, you want to know which date is later, and which is earlier. For a human being, of course, that’s such and easy task that you generally don’t even need to think about it, and for the most part, it’s not that much more difficult for a computer to do.
How the Computer Does It
But you do need to tell the computer what to do, and how to do it. Most programming languages (including JavaScript) compare dates as numbers, so a later date is a larger number, and an earlier date is a smaller number. They usually can combine dates and times, as well, treating each date + time combination as a number, so 3:31 AM on January 17, 1832 is a smaller number than 7:15 PM on that same date.
Dates In JavaScript
Before we talk about comparing dates, let’s take a quick look at the date object, and how to create a date in JavaScript:
You use the Date() constructor to create a date object. By default (with no arguments between the parentheses), the date object will be set to the current time:
var rightnow = new Date(); document.write(rightnow);
Which will output the current date and time, like this:
Wed May 21 21:44:56 PDT 2014
Setting the Date
To set a date object to a specific date, you use the date as the argument in the Date() constructor:
var backthen = new Date(1953,11,3);
The output will look like this:
Thu Dec 3 00:00:00 PST 1953
Note that the Date() constructor counts the months with January as 0 (so that 11 = December), but the year and day match the standard calendar year and day.
There are, needless to say, a number of variation on setting the date and time, but the examples shown here should be enough to get you started with comparing dates.
Basic Date Comparison
So how do you compare dates? Take a look at this code:
var rightnow = new Date(); var backthen = new Date(1953,11,3); if (rightnow>backthen) { document.write(rightnow + " is later than " + backthen + "."); } else { document.write("Oh, no! Time is reversed! " + backthen + " is later than " + rightnow + "!"); }
And the output, not surprisingly (unless you do happen to live in a time-reversed version of this universe), is:
Wed May 21 23:03:16 PDT 2014 is later than Thu Dec 3 00:00:00 PST 1953.
How It Works
As you can see, the comparison itself is pretty simple, and it’s pretty typical of most comparisons in most current programming language. It just uses an “if” conditional statement, along with the basic comparison operators (+, >, and <): “If rightnow is greater than backthen”…
In practice, of course, you’d probably be comparing dates that weren’t hard-coded, and you’d be doing something other than just printing out the results. Let’s consider some of the ways that you can put date comparison in JavaScript to practical use.
Starting and Ending
Suppose an online store is running a promotion — all merchandise is 20% off from July 1, 2015 through July 20, 2015, and you need to write a JavaScript routine that checks to see if the current date falls within the promotional period.
var rightnow = new Date(); var startdate = new Date(2015,06,01); var enddate = new Date(2015,06,21); if (rightnow>=startdate) { if (rightnow<enddate){ document.write("The promotional 20% discount is in effect.");} } else { document.write("The special July promotion is not in effect at this time."); }
Details Are Important
Note that we set startdate and enddate without including the time. This sets the time to the default — roughly a millisecond after midnight at the start of the day in question. We also set enddate to the 21st, so that all of July 20th will be included (along with a millisecond of the 21st, which we won’t worry about). And of course, keep in mind that 2015,06,01 is July 1, and not June 1, since we count the months from January = 0.
Age Check
What else can you do with date comparisons? Consider a school admissions office. If there’s a minimum age for student eligibility to take classes without parental permission, you can compare incoming students’ birthdays with a cutoff birth date — any students born after that date will be too young to take classes without written permission form their parents.
But… That date will always be changing — it will always be 18 years to the day before the current date. How do you compare incoming student birthdays with a moving target? As it turns out, you can use the date object’s setFullYear() method to automatically set the year of the cutoff date to the current year minus 18, so every time the program checks a student’s birthday, it compares it to the correct cutoff date.
Sales Records
What if you need to search through sales records to find all transactions on, or before, or after a certain date, or between two dates? The actual dates may (and probably will) be different for each search, so you allow the user to input the search dates, and then do the comparison for each record. In this case, none of the dates are hard-coded; the sales dates are stored in a database as part of the sales records, and the comparison dates are supplied by the user. this is typical of many real-life situations in which you will write code to do date comparisons.
There are, needless to say, plenty of other uses for date comparisons in JavaScript, and there are plenty of opportunities for skilled JavaScript programmers, web developers, information technology professionals, and people with advanced computer skills in general.
Recommended Articles
Top courses in JavaScript
JavaScript students also learn
Empower your team. Lead the industry.
Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.