Time formatting in JavaScript

In JavaScript, the time formatting can be done by using the date object. If you create a date object without parameters, it will return the current date and time. Besides date object has time-related methods that you can use to format time in JavaScript.

e.g : var d = new date();

This tutorial will show you examples of how to get current time by using date object along with time methods.

Timestamp: current time example

Following is an example of getting the timestamp. As you click on the button, the current date and time will display in an alert. We simply created a date object without any parameters and shown this in an alert. See the example by clicking the link below along with code:

We simply created a date object without any parameters and shown this in an alert. See the example by clicking the link below along with code:

Experience this example online

As you can see, the date and time are shown in an alert.

getHours method example

Following example will return the current hour only, by using the getHours method of the date object.

Experience this example online

Get current minutes example

Following example returns current minutes from the local system by using the getMinutes() method for JavaScript time formatting.

Experience this example online

JavaScript timestamp example in hh: mm seconds format

Following is an example of JavaScript timestamp in “hh: mm seconds” format.  The current time will be taken from the local system. Click on the link or image below to see it online:

Experience this example online

JavaScript getTime example

The getTime method returns the time in milliseconds since January 1, 1970.  The following example gets the time in Milliseconds by using the getTime method.

Experience this example online

Also see: JavaScript date