javascript time

Time formatting in javascript

In JavaScript, 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 is shown 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:

Experience this example online


As you can see date and time is 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.

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 link 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 time in Milliseconds by using JS getTime method.

Experience this example online


Also see: Javascript date

Was this article helpful?

Related Articles

Leave A Comment?