In theory, administration dates as a developer is as simple as creating, storing, and, if necessary, manipulating dates. But as a JavaScript developer, you would know this theory doesn’t hold long after you start alive with dates for real. On top of altered date-time formats, you have to accede timezone and local differences.

For this reason, plenty of JavaScript developers seek help from third-party libraries when they have to manage dates in an application. While these libraries reduce the task’s complexity, having a clear compassionate of administration boilerplate JavaScript dates has its benefits.

This tutorial will acquaint you to alive with dates in boilerplate JavaScript, as well as useful third-party libraries to help you abridge more circuitous date-related tasks.

JavaScript Date object

The Date object in JavaScript is the main aspect when it comes to administration date and time. It annal a single point in time as the milliseconds’ number delayed since the 1st January 1970 00:00:00 (UTC). This date-time aggregate is known as the epoch time. As far as JavaScript is concerned, it’s the alpha of time in the world.

Creating Dates

You can simply create a date using new Date() . You can pass ambit to the Date architect to create a date of your choice. The given constant can take altered forms.

Pass a date string

You can pass a date string of an accustomed format when creating a new Date object.

Now, if we print the created date, it shows this.

In accession to the date we passed, the date object has more values, including a time and a timezone. Since we didn’t give a specific value for these ambit when creating the object, JavaScript uses the local time and timezone of the code’s system.

If we want to pass the time or timezone with the constant string, we can use a format like this.

YYYY-MM-DDTHH:mm:ss.sssZ

  • YYYY: year
  • MM: month (1 to 12)
  • DD: date (1 to 31)
  • HH: hour in 24-hour format (0 to 23)
  • mm: account (0 to 59)
  • ss: abnormal (00 to 59)
  • sss: milliseconds (0 to 999)
  • T is used to abstracted the date and time in the string
  • If Z is present, the time is affected to be in UTC. Otherwise, it assumes the local time.

However, if T and Z are not present, the string’s created date may give altered after-effects in altered browsers. In that case, to always have the same timezone for the date, add  HH:mm or -HH:mm to the end.

webrok

You can get the same after-effects using the Date.parse function instead of casual the date string to the Date constructor. Date.parse is alongside being called inside the architect whenever you pass a date string.

The format used in these strings is the ISO 8601 agenda continued format. You can refer to its capacity in the ECMAScript specification .

Pass date arguments

You can anon pass the date arguments to the Date architect after using ambagious date strings. The order and length of each year, month, etc., are absolutely as in a date string.

webrok

When we audit the created date’s outcome, we can notice one acute aberration in the final date.

webrok

What’s weird? When we created the date, we used 9 for the month, which we could assume to be September. However, when we print the result, the month is October instead. Why is that?

JavaScript uses a zero-based index to analyze each month in a year. This means, for JavaScript, January is represented by 0 instead of 1. Similarly, October is represented by 9 instead of 10.

In this method of creating a date, we can’t pass an altercation to announce its time zone. So, it’s defaulted to the local time of the system. But we can use the Date.UTC function to catechumen the date to UTC before casual it to the Date constructor.

webrok

Pass a timestamp

Remember that I mentioned JavaScript stores the time delayed since the epoch time in the Date object? We can pass this delayed time value, called a timestamp, to announce the date we are creating.

webrok

Create a Date object for the accepted date and time

If you want to create a Date object for the accepted date and time of the system, use the Date architect after casual any argument.

webrok

You can also use the Date.now() function for the same task.

webrok

Formatting dates

JavaScript provides several congenital functions to format a date. However, these functions only catechumen the date to a format specific to each one.

Let’s see how each formatting action works.

webrok

Internationalization API

ECMAScript Internationalization API allows the formatting of a date into a specific locale using the Intl object.

webrok

You can pass an options object to the DateTimeFormat action to affectation time values and adapt the output.

webrok

Custom date formats

If you want to format the date to any other format beyond what these functions provide, you’ll have to do so by accessing each part of the date alone and accumulation them.

JavaScript provides the afterward functions to retrieve the year, month, date, and day from a Date object.

webrok

Now, you can catechumen the date to a custom format using retrieved parts.

Updating dates

JavaScript provides several methods to edit an already created date.

webrok

Comparing dates

If you want to know whether a specific date comes before another, you can use greater than and less than operators anon for comparison.

webrok

This is because Dates in JavaScript are objects, so each date has a altered instance of the class, and the == or === operator are comparing the memory abode instead of the actual values of the dates.

JavaScript date abetment libraries

We can find several JavaScript date and time abetment libraries as open-source projects or otherwise. Some of them, advised for all kinds of date-time manipulations, and some have a specific set of use cases. In this section, I’ll only talk about accepted multi-purpose libraries.

Moment.js used to be the king of date abetment libraries among JavaScript developers. However, its developers afresh announced that it’s absorption on advancement the accepted codebase instead of adding new features. They acclaim attractive for an another band-aid for those who are alive on new projects.

So, apart from Moment.js, what are the libraries we can use to make our life easier as developers?

Date-fns

Date-fns in an open-source library acknowledging date parsing and formatting, locales, and date accession like accession and subtraction. It’s dubbed as Lodash for dates due to its versatility.

webrok

Luxon

Luxon is a date-time abetment library created by one of the Moment.js developers to suit modern appliance requirements. Similar to Date-fns, Luxon offers data formatting and parsing functions. Also, it has native Intl abutment and is chainable.

webrok

Summary

This tutorial discussed how to work with date and time in JavaScript with and after alien libraries. Alive with dates is always aching in almost (if not all) programming languages. Fortunately for us, JS and its ecosystem of libraries does all the heavy work for us, acceptance us to focus on architecture features.

This article was originally appear on Live Code Stream by Juan Cruz Martinez (twitter: @bajcmartinez), architect and administrator of Live Code Stream, entrepreneur, developer, author, speaker, and doer of things.

Read next: Swarms of robot fish could soon adviser our oceans for ecology hazards