Gice

Technology and General Blog

JavaScript has a built-in datatype that creates dynamic dates or stores current, previous, or future dates. Date objects can be created by using the new Date() method.

Once the date object is created, you can apply different operations to it. You can show a timer on your website using the date object.

The date in JavaScript is the number of milliseconds passed since the midnight of January 1, 1970, UTC. It is important to note that although the date in JavaScript is based on UTC when the JavaScript code is executed on a browser, it fetches the time zone from the host system:

Note: We will use the browser console to demonstrate examples performed in this post. To open up the browser console:

  • Use the F12 key in Chrome and other chromium-based browsers.
  • Use CTRL + SHIFT + K keyboard shortcut keys for Mozilla.
  • Use Option + ⌘ + C keyboard shortcut keys in Safari (if developer menu does not appear, then open Preferences by pressing ⌘ +, and in Advanced tab check “Show Develop menu in menu bar”).

How to create a Date Object in JavaScript?

As mentioned above, a date object can be created with the new Date() method. To use a date object in JavaScript, we first need to create a variable and then store its date. The date in JavaScript is displayed as a full-text string.

var date = new Date();
console.log(date);

The new Date() function without any parenthesis creates an object with the current date. We can also pass arguments to the new Date() function to create new date objects with any specified date and time. The arguments use the following syntax in the new Date() function:

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

Example:

var date = new Date(2001, 10, 10, 01, 37, 50, 50);

console.log(date);

In the above example, the 10th month is November; this is because JavaScript starts counting months from 0. So January is the 0th month in JavaScript.

We can pass (minimum) 1 to (maximum) 7 arguments to the new Date() function.

Now let’s try with five parameters:

new Date(year, month, day, hours, minutes)
var date = new Date(2001, 10, 10, 01, 37);

console.log(date);

The seconds are 00 by default.

In the same way, we can also give two, three, four, and six arguments. The order of the arguments will always remain the same unless we are only giving a single argument.

If only one parameter is supplied to the new Date() function, it will be considered as milliseconds:

var date = new Date(2001);

console.log(date);

The time shown in the screenshot above is 2001 milliseconds past midnight of January 01, 1970.

One or two-digit years will be considered from the previous century:

var date = new Date(9, 5);

console.log(date);

We can also pass a date string to the new Date() function:

var date = new Date(“November 10, 2001 01:37:50:50”);

console.log(date);

JavaScript Date Methods:

In JavaScript, we can use different methods to perform different actions on a date object. A few of these methods are discussed here:

The getFullYear() method:

The getFullYear() method can be used to get the year from the date object.

var date = new Date();

console.log(date.getFullYear());

The getMonth() method:

The getMonth() method returns the value of the month in the date object.

var date = new Date();

console.log(date.getMonth());

Note: This value is always one less than the actual month.

Similarly the getDate(), getHours(), getMinutes(), getSeconds() and getMilliseconds() can be used to get the individual values of Date, Hours, Minutes, Seconds and Milliseconds respectively.

The getTime() method:

The getTime method can be used to get the number of milliseconds that have passed since midnight Jan 1, 1970:

var date = new Date();

console.log(date.getTime());

The getDay() method:

The getDay() method is used to get the number of the weekdays in the date object. Days in JavaScript are numbered from 0 to 6, and the week starts from Sunday.

var date = new Date();

console.log(date.getDay());

The setFullYear() method:

The setFullYear() method is used to set the year of the date object. It can optionally set the month and the day of the date object as well.

var date = new Date();

console.log(date.getFullYear());

date.setFullYear(1993);

console.log(date.getFullYear());

The setMonth() method:

The setMonth() method is used to set the month in the date object.

var date = new Date();

console.log(date.getMonth());

date.setMonth(8);

console.log(date.getMonth());

Similarly the setDate(), setHours(), setMinutes(), setSeconds() and setMilliseconds() can be used to set the individual values of Date, Hours, Minutes, Seconds and Milliseconds (respectively) in the date object.

The setTime() method:

The setTime method can be used to set time in milliseconds after midnight of Jan 01, 1970.

var date = new Date();

console.log(date);

date.setTime(800000);

console.log(date);

Conclusion

Date object is an inbuilt data type that is used to store and display dates in JavaScript. Whenever you see a time counter or a countdown on a webpage, there is a date object behind it. Date objects are extremely useful when working with dates in JavaScript.

In this post, we discussed what a date object is and how to create it. Moreover, we also learned some methods which we can use to operate on a date object and manipulate it according to our needs.

Date objects can be very confusing and difficult to work with. Many JavaScript developers opt for third-party libraries instead of the inbuilt date object when they need to use dates in their code.

Leave a Reply

Your email address will not be published. Required fields are marked *