Javatpoint Logo
Javatpoint Logo

JavaScript Time Now

JavaScript Date.now() Method

JavaScript Date.now() function is used to display the time elapsed since January 1, 1970, 00:00:00 UTC. It will return the value in milliseconds. Since now() is a static method of the Date object, it will always be used as Date.now(). We cannot use them like myDate.now() or with any other method.

Syntax:

Parameters:

The Date.now() method does not require any parameter values.

Return Values:

It returns the time elapsed since January 1, 1970, 00:00:00 UTC in milliseconds.

How to Get Current Date and Time in JavaScript?

The Date() method generates a new Date object having the current date and time. Once we have a date object, we can integrate any of the available methods, such as getFullYear(), to extract its properties; it will return a 4-digit year.

If we call the Date() method without any arguments is equivalent to new Date(Date.now()).

The following are some common date methods:

Get the Current Year

The getFullYear() method is used to get the current year. Consider the below example:

Example:

Output:

2022

Get the Current Month

The getMonth() method is used to get the current month. It will return the value from 0 to 11, which indicates the months from January to December. So to get the correct month value, add +1 to the output value. Consider the below example:

Example:

Output:

5

From the above example, the output is 5, which is the 6th month (June) of the calendar.

Get the current day

The getDate() method returns the current date of the month. Consider the below example:

Example:

Output:

10

From the above output, it is returning today's date.

Get the current hour

The getHours() method returns the current hour from the current time. For example, if the current time is 12:30 pm, it will return 12. Consider the below example:

Example:

Output:

13

Get the current minutes

The getMinutes() method returns the current minutes from the current time. For example, if the current time is 12:30 pm, it will return the 30. Consider the below example:

Example:

Output:

35

Get the current seconds

The getSeconds() method returns the current second from the current time. For example, if the current time is 12:30:20 pm, it will return the 20. Consider the below example:

Example:

Output:

20

Get the current milliseconds

The getMilliseconds() method returns the value of current milliseconds from the current time. For example, if the current time is 12:30:20:991 pm, it will return the 991. The millisecond's value ranges from 0 to 999 in an instance of a Date object.

Consider the below example:

Example:

Output:

991

Thus, we can get the different instances of a date object, including the current date & time, using some date methods.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA