JavaScript Date

The Date object in JavaScript is used to get the current date. We can also get the day, month, year, time, etc. The new keyword is used to create a Date object. Let us see how to create a Date object and work around the Date methods.

Create a Date object

In JavaScript, we can create a Date object using the new keyword. We use the new Date() constructor.

Display the current date and time

Let us see an example to display the current date and time:

Output

Display the current date and time in JavaScript

Display date and time from day, month, and year

Let us see another example to display the complete date and time from a specific date. The input we have given is the day, month, and year:

Output

Display date and time from day, month, and year in JavaScript

Date Methods

Different methods are provided by JavaScript to work on Date operations. Let’s see them one by one:

JavaScript Date Methods

Let us see the methods with examples:

JavaScript getDate() method

The getDate() method returns the day of the month. The value range is from 1-31. Let us see an example:

Output

JavaScript getDate() method

JavaScript getDay() method

The getDay() method returns the day of the week. The value range is from 0-6. Let us see an example:

Output

JavaScript getDay() method

JavaScript getFullYear() method

The getFullYear() method in JavaScript returns the complete year, i.e. 2023, 2022, etc. Let us see an example:

Output

JavaScript getFullYear() method

JavaScript getHours() method

The getHours() method in JavaScript returns the hour. The value range is from 0-23. Let us see an example:

Output

JavaScript getHours() method

JavaScript getMilliseconds() method

The getMilliseconds() method in JavaScript returns the milliseconds. The value range is from 0-999. Let us see an example:

Output

JavaScript getMilliseconds() method

JavaScript getMinutes() method

The getMinutes() method in JavaScript returns the minutes. The value range is from 0-59. Let us see an example:

Output

JavaScript getMinutes() method

JavaScript getMonth() method

The getMonth() method in JavaScript returns the month. The value range is from 0-11. Let us see an example:

Output

JavaScript getMonths() method

JavaScript getSeconds() method

The getSeconds() method in JavaScript returns the seconds. The value range is from 0-59. Let us see an example:

Output

JavaScript getSeconds() method

JavaScript setFullYear() method

The setFullYear() method in JavaScript sets the year of a date object. Let us see an example:

Output

JavaScript setFullYear() method

JavaScript setHours() method

The setHours() method in JavaScript sets the hour of a date object. Let us see an example:

Output

JavaScript setHours() method

JavaScript setMilliseconds() method

The setMilliseconds() method in JavaScript sets the milliseconds of a date object. Let us see an example:

Output

JavaScript setMilliseconds() method

JavaScript setMinutes() method

The setMinutes() method in JavaScript sets the minutes of a date object. Let us see an example:

Output

JavaScript setMinutes() method

JavaScript setMonth() method

The setMonth() method in JavaScript sets the month of a date object. Let us see an example:

Output

JavaScript setMonth() method

JavaScript setSeconds() method

The setSeconds() method in JavaScript sets the seconds of a date object. Let us see an example:

Output

JavaScript setSeconds() method

JavaScript toString() method

The toString() method in JavaScript is used to return the string representation of the date object. Let us see an example:

Output

JavaScript toString() method

JavaScript Arrays
JavaScript - Hoisting
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading