JavaScript Time Now

JavaScript Date.now Method

The Date.now function in JavaScript serves the purpose of indicating the amount of time that has passed since January 1, 1970, at 00:00:00 UTC. The returned value is expressed in milliseconds. As now is a static method belonging to the Date object, it must be invoked in the form of Date.now. It cannot be utilized in the manner of myDate.now or in conjunction with any other methods.

Syntax:

Example

Date.now();

Parameters:

The Date.now function does not take any arguments.

Return Values:

It provides the duration that has passed since January 1, 1970, at 00:00:00 UTC, measured in milliseconds.

How to Get Current Date and Time in JavaScript?

The Date function creates a fresh Date object that reflects the present date and time. After obtaining a date object, we can utilize any of the methods provided, such as getFullYear, to retrieve its attributes; this method will yield a year in a four-digit format.

Invoking the Date method without providing any parameters is functionally the same as executing new Date(Date.now).

The following are some common date methods:

Get the Current Year

The getFullYear function serves the purpose of retrieving the current year. Take a look at the example provided below:

Example:

Example

function myFunction() {

  var currentYear= (new Date()).getFullYear();

console.info(currentYear);

}

myFunction();

Output:

Get the Current Month

The getMonth function is utilized to retrieve the current month. Its return value ranges from 0 to 11, representing the months starting from January through December. Therefore, to obtain the accurate month value, you should add +1 to the result. Take a look at the example provided below:

Example:

Example

function myFunction() {

  var currentMonth= (new Date()).getMonth();

console.info(currentMonth);

}

myFunction();

Output:

In the previous example, the result is 5, corresponding to the 6th month of the year, which is June.

Get the current day

The getDate function retrieves the current day of the month. Examine the example provided below:

Example:

Example

function myFunction() {

  var currentDay = (new Date()).getDate();

console.log(currentDay);

}

myFunction();

Output:

The output provided above yields the current date.

Get the current hour

The getHours function retrieves the hour from the present time. For instance, if the current time is 12:30 PM, it will provide the value 12. Examine the following example:

Example:

Example

function myFunction() {

  var currentHours = (new Date()).getHours();

console.log(currentHours);

}

myFunction();

Output:

Get the current minutes

The getMinutes function provides the minute component from the current time. For instance, if the time is 12:30 PM, the method will yield 30. Observe the example below:

Example:

Example

function myFunction() {

 var currentMinutes = (new Date()).getMinutes();

console.log(currentMinutes);

}

myFunction();

Output:

Get the current seconds

The getSeconds function retrieves the current second from the present time. For instance, if the time reads 12:30:20 PM, it will return the value 20. Take a look at the example below:

Example:

Example

function myFunction() {

 var currentSeconds = (new Date()).getSeconds();

console.log(currentSeconds);

}

myFunction();

Output:

Get the current milliseconds

The getMilliseconds function retrieves the current millisecond value from the present time. For instance, if the time reads 12:30:20:991 PM, it will yield the value of 991. In a Date object, the millisecond value can vary from 0 to 999.

Consider the below example:

Example:

Example

function myFunction() {

 var CurrentMilliseconds = (new Date()).getMilliseconds();

console.log(CurrentMilliseconds);

}

myFunction();

Output:

Therefore, we can obtain various instances of a date object, which encompass the present date and time, by utilizing several date methods.

Input Required

This code uses input(). Please provide values below: