Quick Reach
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<!DOCTYPE html>
<html>
<body>
<button onclick=“showdate()”>javascript current date and time</button>
<script type=“text/javascript”>
function showdate(){
var JSdate = new Date();
alert (JSdate);
}
</script>
</body>
</html>
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<!DOCTYPE html>
<html>
<body>
<button onclick=“showdate()”>Show current hour</button>
<script type=“text/javascript”>
function showdate(){
var JSdate = new Date();
alert (JSdate.getHours());
}
</script>
</body>
</html>
|
Get current minutes example
Following example returns current minutes from the local system by using the getMinutes() method.
Experience this example online
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<!DOCTYPE html>
<html>
<body>
<button onclick=“showdate()”>Show current minute</button>
<script type=“text/javascript”>
function showdate(){
var JSdate = new Date();
alert (JSdate.getMinutes());
}
</script>
</body>
</html>
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<!DOCTYPE html>
<html>
<body>
<button onclick=“showdate()”>Show current time in hh:mm seconds format</button>
<script type=“text/javascript”>
function showdate(){
var JSdate = new Date();
alert (JSdate.getHours() + “:” + JSdate.getMinutes() + ” “ + JSdate.getSeconds());
}
</script>
</body>
</html>
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<!DOCTYPE html>
<html>
<body>
<button onclick=“showdate()”>javascript getTime method</button>
<script type=“text/javascript”>
function showdate(){
var JSdate = new Date();
alert (JSdate.getTime());
}
</script>
</body>
</html>
|
Also see: Javascript date
Leave A Comment?