JavaScript Date and Time


Creating date object

A native JavaScript object is the Date object. By using the browser to access the computer's system clock, you may obtain the user's local time. Moreover, the Date object offers a number of ways to organise, change, and format dates and times.

We must first build a Date object before we can operate with the date and time. Date objects must be constructed using the Date constructor function, which is Date, as dates don't have a matching literal form like other built-in objects like arrays or functions have ().


1. New date syntax

A new Date object can be declared without having to initialize its value. The user's device on which the script is being run will be used to set the date and time value in this instance to the current date and time.

Example

Preview

2. New date Year, Month

Moreover, you may initialize a Date object by giving the year, month, day, hours, minutes, seconds, and milliseconds arguments, which are separated by commas. The criteria for the year and month must be provided; all other elements are optional.

Example

Preview

3. New date string

You may also build a Date object in JavaScript by supplying a string that represents a date or a date and time.

Example

Preview

4. New date Milliseconds

Moreover, you may construct a Date object by providing the milliseconds that have passed since January 1 at 00:00:00 GMT. The UNIX operating system was initially debuted in 1970, which is why this period of time is referred to as the UNIX epoch. Once you have an instance of the Date object, you may use its methods to carry out a variety of operations, including accessing different date components, setting or updating specific date and time values, etc. The next sections go into great depth about these techniques.

Example

Preview

Get the current date & time

Create a new Date object without supplying any arguments to obtain the time and date as they are right now. The current date and time will be added to the object as a result.

Example

Preview

Creating date & time string

Several methods, including toDateString() and toLocaleDateString(), are available in the JavaScript Date object to produce date strings in various formats. The toLocaleTimeString() and toTimeString() methods of the Date object can also be used to create time strings.

Example

Preview

Get the year month and date

You may extract individual date components from the Date object, such as the year, day of the month, day of the week, etc., using one of the methods provided by the Date object, such as getFullYear(), getMonth(), getDay(), etc. Instead of returning a name like Sunday or Monday, the getDay() function produces a number reflecting the day of the week (from 0 to 6). If it is Sunday, the method returns 0; if it is Monday, the method returns 1, and so on. You may extract individual date components from the Date object, such as the year, day of the month, day of the week, etc., using one of the methods provided by the Date object, such as getFullYear(), getMonth(), getDay(), etc. Instead of returning a name like Sunday or Monday, the getDay() function produces a number reflecting the day of the week (from 0 to 6). If it is Sunday, the method returns 0; if it is Monday, the method returns 1, and so on.

Example

Preview

Get hours, minutes, seconds and milliseconds

Similar functions are available on the Date object, including getHours(), getMinutes(), getSeconds(), and getTimezoneOffset(). The 24-hour clock's equivalent of the number of hours in the day (from 0 to 23) is returned by the getHours() function. As a result, the procedure returns 0 at midnight and 15 at 3 o'clock in the afternoon.

Example

Preview

Setting the year month & date

The year, month, and date components of the Date object can each be set using the setFullYear(), setMonth(), and setDate() methods provided by the Date object. For instance, in the example below, the current date recorded in a variable was changed to a date two years in the future using the setFullYear() function. If you enter a number for the month that is more than 11 using the setMonth() function, the year value of the date object will increase. In other words, a number of 12 causes the month value to be set to 0 and the year value to increase by 1.

Example

Preview

Setting the hours minutes & seconds

The procedures for establishing the time values are likewise rather simple. The hour, minutes, seconds, and milliseconds parts of the Date object can be set using the setHours(), setMinutes(), setSeconds(), and setMilliseconds() methods, respectively. Each method accepts arguments that are integer numbers. There are 0 to 23 hours. The range of minutes and seconds is 0 to 59. The range of milliseconds is 0 to 999.

Example

Preview