Reusable User-defined Date & Time Functions in JavaScript

Reema Alzohairi
8 min readFeb 4, 2020

--

Photo by Murray Campbell on Unsplash

Comparing & formatting date & time are two crucial functionalities to any programmer. In this post, I’m sharing with you reusable user-defined JavaScript date & time functions that I have formed throughout different challenges using different resources. I gotchyou, worry not.

Hopefully this can help you save a lot of time and make date & time manipulation easier for you. You can even use these functions as a starting point to create other general-purpose or custom functions.

Before starting, you must be familiar with the look and feel of date objects in JavaScript. The basic idea you need to know is how to create a date object, which is done using the built-in date constructor new Date().

Simply using the constructor with no parameters creates a date object of the current date & time. However, a few parameter variations are available as shown in MDN docs and W3Schools.

In this post, we will be using the constructer the following way:

new Date(year, month, day, hours, minutes, seconds)

Note: The month value ranges from 0 to 11, where 0 is January and 11 is December. This is a very important point to remember whenever you’re creating a date object.

You will see other built-in date functions used in some of the examples, such as getFullYear(), getMonth() and getDate(), which returns the year as 4 digits, the month (0–11) and the day date (1–31), respectfully.

.isSameYear( ) — Determines if the two passed dates are in the same year

  • Input: date1 and date2.
  • Output: true if the two passed dates are in the same year and false otherwise.
  • Example: isSameYear(new Date(2018, 3, 18, 10, 3, 0), new Date(2018, 6, 5, 10, 3, 0)) returns true, whereas isSameYear(new Date(2007, 3, 18, 10, 3, 0), new Date(2018, 6, 5, 10, 3, 0)) returns false.

.isSameMonth( ) — Determines if the two passed dates are in the same month

  • Input: date1 and date2.
  • Output: true if the two passed dates are in the same month and false otherwise.
  • Example: isSameMonth(new Date(2007, 5, 18, 10, 3, 0), new Date(2018, 5, 5, 10, 3, 0)) returns true, whereas isSameMonth(new Date(2007, 1, 18, 10, 3, 0), new Date(2018, 5, 5, 10, 3, 0)) returns false.

Note: Function only compares the month, not the year.

.isSameDayDate( ) — Determines if the two passed dates are on the same day date i.e. 18

  • Input: date1 and date2.
  • Output: true if the two passed dates are on the same day date and false otherwise.
  • Example: isSameDayDate(new Date(2007, 5, 18, 10, 3, 0), new Date(2018, 6, 18, 10, 3, 0)) returns true, whereas isSameDayDate(new Date(2007, 1, 18, 10, 3, 0), new Date(2018, 5, 17, 10, 3, 0)) returns false.

Note: Function only compares the day date, not the year nor the month.

.isSameHour( ) — Determines if the two passed dates have the same hour value

  • Input: date1 and date2.
  • Output: true if the two passed dates have the same hour value and false otherwise.
  • Example: isSameHour(new Date(2007, 5, 18, 10, 3, 0), new Date(2018, 5, 18, 10, 3, 0)) returns true, whereas isSameHour(new Date(2007, 1, 18, 10, 3, 0), new Date(2018, 5, 17, 9, 3, 0)) returns false.

.isSameMinute( ) — Determines if the two passed dates have the same minute value

  • Input: date1 and date2.
  • Output: true if the two passed dates have the same minute value and false otherwise.
  • Example: isSameMinute(new Date(2007, 5, 18, 9, 30, 0), new Date(2018, 5, 18, 10, 30, 0)) returns true, whereas isSameMinute(new Date(2007, 1, 18, 10, 5, 0), new Date(2018, 5, 17, 10, 30, 0))returns false.

Note: Function only compares the minute value, not the hour value.

.isDateRightNow( ) — Determines if the passed date is the date for the moment of right now

  • Input: a date.
  • Output: true if the passed date is the date for the moment of right now and false otherwise.

Example: isDateRightNow(new Date()) returns true, whereas isDateRightNow(new Date(2018, 3, 18, 10, 3, 0)) returns false.

Note: Function compares up to minutes and the date of right now is the timestamp at the moment of function execution.

.isDateToday( ) — Determines if the passed date is the date of today

  • Input: a date.
  • Output: true if the passed date is the date of today and false otherwise.

Example: isDateToday(new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate(), 10, 3, 0)) returns true, whereas isDateToday(new Date(2018, 3, 18, 10, 3, 0)) returns false.

Note: The date of today is the timestamp at the moment of function execution.

.isDateYesterday( ) — Determines if the passed date is the date of yesterday

  • Input: a date.
  • Output: true if the passed date is the date of yesterday and false otherwise.

Example: isDateYesterday(yesterdaysDate) returns true, whereas isDateYesterday(new Date()) returns false.

Note: The value of yesterdaysDate must be the date of yesterday, with respect to the date at the moment of function execution

.getYesterdaysDate( ) — Returns the date of yesterday

  • No Input.
  • Output: the date of yesterday.

Note: The returned date of yesterday will be relative to the date at the moment of function execution.

.isDateThisMonth( ) — Determines if the passed date is in the current month

  • Input: a date.
  • Output: true if the passed date is in the current month and false otherwise.

Note: Function compares the month AND the year. Also, the comparison will be relative to the date at the moment of function execution.

.areDatesIdentical( ) — Determines if the passed dates are identical

  • Input: date1 and date2.
  • Output: true if the two passed dates are identical and false otherwise.

Example: areDatesIdentical(new Date(2018, 3, 18, 10, 3, 0), new Date(2018, 3, 18, 10, 3, 0)) returns true, whereas areDatesIdentical(new Date(2018, 3, 18, 10, 2, 0), new Date(2018, 3, 18, 10, 3, 0)) returns false.

.formatDateToHHMM( ) — Converts date to the format HH:MM for 12-hour clock

  • Input: a date.
  • Output: the date in the format HH:MM with AM or PM.

Example: formatDateToHHMM(new Date(2018, 0, 8, 10, 3, 0)) returns "10:03 AM" whereas formatDateToHHMM(new Date(2018, 0, 8, 22, 3, 0)) returns "10:03 PM".

Note: Single digits will be prefixed with a zero.

.formatDateToHHMMSS( ) — Converts date to the format HH:MM:SS for 12-hour clock

  • Input: a date.
  • Output: the date in the format HH:MM:SS with AM or PM.

Example: formatDateToHHMMSS(new Date(2019, 3, 18, 10, 50, 1)) returns "10:50:01 AM" whereas formatDateToHHMMSS(new Date(2019, 3, 18, 22, 50, 55)) returns "10:50:55 PM".

Note: Single digits will be prefixed with a zero.

.formatDateToDDMMYY( ) — Converts date to the format DD/MM/YY

  • Input: a date and a boolean to determine whether or not to prefix single digits with a zero.
  • Output: the date in the format DD/MM/YY.

Example: formatDateToDDMMYY(new Date(2019, 3, 18, 10, 3, 0), false) returns "18/4/19" whereas formatDateToDDMMYY(new Date(2019, 3, 18, 10, 3, 0), true) returns "18/04/19".

.formatDateToMonthNameAndDay( ) — Converts date to the format "monthName dayNumber"

  • Input: a date and a boolean to determine whether or not to abbreviate the month’s name.
  • Output: the date in the format "monthName dayNumber".

Example: formatDateToMonthNameAndDay(new Date(2019, 3, 18, 10, 3, 0), false) returns "April 18" whereas formatDateToMonthNameAndDay(new Date(2019, 3, 18, 10, 3, 0), true) returns "Apr 18".

.sortListOfDates( ) — Sorts the passed list of dates based on the order preference

  • Input: a list of dates and a boolean to determines whether or not to sort the list in an ascending order.
  • Output: the list sorted based on order preference.

Note: The list is sorted in place, meaning the original list is sorted and not a new list. So, there is no need for this function to return the sorted list. However, it returns the list for unit test purposes.

.getTextRepresentationOfTimestamp( ) — Returns the proper text representation of the passed date

  • Input: a date.
  • Output:
1. If the timestamp is right now, "Now" will be returned.2. Else if the date is today's date, the date in the format "HH:MM PM/AM" will be returned.3. Else if the date is yesterday's date, "Yesterday" will be returned.4. Else if the date is in this month, the date in the format "monthName dayDate" with no abbreviation will be returned.5. If no condition applies, it will return the date in the format "DD/MM/YY".

Note: This is a purely custom function fully based on preference, definitely modify it to your liking if you wish.

Source Code

On Github, I have created 2 repositories. The first repository is a JavaScript repo. You can clone or download the project using the following Github link.

The second repository is a TypeScript repo, which is an Angular project. You can clone or download the project using the following Github link.

--

--