In this article, you’ll learn everything you need to know about working with dates in JavaScript. So, without further ado, let’s hop in.
How to Create Date Objects
You can create a date object in JavaScript using the following four ways:
1. new Date()
The new Date() constructor creates a date object with the current date and time.
Output:
2. new Date(datestring)
The new Date(datestring) constructor creates a date object from a date string.
Output:
3. new Date(milliseconds)
The new Date(milliseconds) constructor creates a date object by adding the milliseconds to the zero time. The parameter represents the time passed in milliseconds since 1 January 1970 UTC.
Output:
4. new Date(year, month, day, hours, minutes, seconds, milliseconds)
The new Date(year, month, day, hours, minutes, seconds, milliseconds) constructor creates a date object with a specified date and time. The seven parameters specify the year, month, day, hours, minutes, seconds, and milliseconds respectively.
Output:
You can also create a date object with six, four, three, or two parameters.
a) The six parameters specify year, month, day, hour, minute, and second.
b) The five parameters specify year, month, day, hour, and minute.
c) The four parameters specify year, month, day, and hour.
d) The three parameters specify year, month, and day.
e) The two parameters specify year and month.
Note:
JavaScript counts months from 0 to 11 i. e, January is represented by 0, and December is represented by 11. If you specify out-of-range values in the Date object, it will not throw an error instead, it auto-corrects itself.
Output:
JavaScript Date Get Methods
You can use the following methods to get information from a date object.
1. getFullYear()
This method returns the year as a four-digit number (yyyy) according to local time.
Output:
2. getMonth()
This method returns the month as a number (0-11) according to local time.
Output:
3. getDate()
This method returns the day as a number (1-31) according to local time.
Output:
4. getHours()
This method returns the hour (0-23) according to local time.
Output:
5. getMinutes()
This method returns the minute (0-59) according to local time.
Output:
6. getSeconds()
This method returns the second (0-59) according to local time.
Output:
7. getMilliseconds()
This method returns the millisecond (0-999) according to local time.
Output:
8. getTime()
This method returns the time in milliseconds since January 1, 1970.
Output:
9. getDay()
This method returns the weekday as a number (0-6) according to local time.
Output:
Note: If you want to work with the UTC dates, you can use the following date methods: getUTCDate(), getUTCDay(), getUTCFullYear(), getUTCHours(), getUTCMilliseconds(), getUTCMinutes(), getUTCMonth(), and getUTCSeconds().
JavaScript Date Set Methods
You can use the following methods to set a part of a date object.
1. setDate()
This method sets the day as a number (1-31) for a specified date according to local time.
Output:
2. setFullYear()
This method sets the year for a specified date according to local time.
Output:
3. setHours()
This method sets the hour (0-23) for a specified date according to local time.
Output:
4. setMilliseconds()
This method sets the milliseconds (0-999) for a specified date according to local time.
Output:
Similarly, you can use the setMinutes(), setMonth(), and setSeconds() methods to set the minutes (0-59), month (0-11), and seconds (0-59) respectively for a specified date according to local time.
JavaScript Current Time Example
A simple example to print the current time of the system:
If you want to have a look at the complete source code used in this article, here’s the GitHub repository.
Develop Projects Using the Date Concepts of JavaScript
You’ll encounter many instances of JavaScript dates while working on real-world applications. If you want to get started with a simple project and gain hands-on experience, you can develop a digital clock using HTML, CSS, and JavaScript. Along with dates, you’ll also learn some important JavaScript concepts like working with variables, using functions, accessing and adding properties to DOM, and more.