JavaScript Date: Create Dates in JavaScript

What does the JavaScript Date constructor do? How can I use it? What are its limitations?

JavaScript has built-in functions to manipulate dates and times. The Date object provides methods to create new instances of the Date class.

The JavaScript Date object provides methods for creating new dates, converting between date formats, comparing two dates, and extracting parts of a date.

In this tutorial, we will see how we can use the Date() object cons

tructor to create new instances of dates according to the parameters we pass to the Date() constructor.

1)- Date() constructor

The Date() object allows us to get back today’

;s date.

The way it works is that we call the Date() functio

n constructor using the new keyword as below:

JavaScript
var todayDate= new Date();

      console.log(todayDate);

As I am writing this tutorial on Saturday December 17th 2022, the result in the browser is:

Today’s date

By default, JavaScript will use the browser’s time zone and display a date as a full-text string.

The Date() object allows us also to create dates by specifying different parameters in the Date() constructor function.

For example, let’s say that we want to create a date of Sunday 25th December 2022.

The code will be:

JavaScript
var todayDate= new Date("2022-12-25");

      console.log(todayDate);

In the browser we have:

Sunday’s date

There are other methods to create new date objects, and the one above is one of them. It is called the new Date(date string) method.

Let’s see the other ones.

a)- new Date(year,month)

The new Date(year, month) method creates a date based on the Year and Month wanted.

Let’s say that we want to create a date of June 2022.

The code is:

JavaScript
var todayDate= new Date(2022, 05);

      console.log(todayDate);

The result is:

Date of June 2022

If you have noticed, in the line of code new Date(“2022”, “05”), the index of the month June is 5, and that is because if we put all the months from January to December in an array, the index of the array starts from 0, which means 0 represents January, so 5 represents June, that’s why in the outcome we have a date of Wednesday, January 01st.

b)- new Date(year,month,day)

The new Date(year, month, day) method creates a date based on the Year, Month and Day wanted.

We will create a date of June 24th, 2022.

JavaScript
var todayDate= new Date(2022, 05, 24);

      console.log(todayDate);

At the end we have:

Date of June 24th 2022

c)- new Date(year,month, day, hours)

The new Date(year, month, day, hours) method creates a date based on the Year, Month, Day and Hours wanted.

We will create a date of June 24th, 2022 at 10 AM.

JavaScript
var todayDate= new Date(2022, 05, 24, 10);

      console.log(todayDate);

So we have:

The date created with the time

d)- new Date(year,month, day, hours, minutes)

The new Date(year, month, day, hours, minutes) method creates a date based on the Year, Month, Day, Hours, and Minutes.

We will create a date of June 24th, 2022 at 10 AM and 44 minutes.

JavaScript
var todayDate= new Date(2022, 05, 24, 10, 44);

      console.log(todayDate);

In the browser we have:

The date with minutes added

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

The new Date(year, month, day, hours, minutes, seconds) method creates a date based on the Year, Month, Day, Hours, Minutes and Seconds.

We will create a date of June 24th, 2022 at 10 AM, 44 minutes and 50 seconds.

JavaScript
var todayDate= new Date(2022, 05, 24, 10, 44, 50);

      console.log(todayDate);

In the browser that will look like this:

The date with seconds added

2)- Previous Century

To create a date of the previous century, for example a date of March 25th, 1958, we can either create it as we learned above by adding the following code:

JavaScript
var todayDate= new Date(1958,02,25);


      console.log(todayDate);

Or by specifying only the 2 last digits for the year like below:

JavaScript
var todayDate= new Date(58,02,25);


      console.log(todayDate);

Both approach give us the same results:

2 digits date

For years from 1900 to 1909 we can add only one digit.

Let’s say that we want to create a date of 01 April 1909.

So the code will be:

JavaScript
var todayDate= new Date(9,03,01);


      console.log(todayDate);

In the end we have:

One digit date

Read More

Inline vs Inline-Block vs Block CSS properties

absolute and relative positions: What’s the difference?

List style type CSS

Tables in HTML

Audio and Video HTML Tutorial – Learn how to add sound effects, videos and Youtube videos to your website

Leave a Reply

%d bloggers like this: