Home » JavaScript » JavaScript Date

JavaScript Dates

The Date object is used to get information about current year, month and day. Firstly, a Date object is created, and then there are many javascript functions available which can operate on that object. These functions gives provide informations like year, month, day, hour, minute, second, and millisecond , using either local time or UTC (universal, or GMT) time.

Date objects are created with the new Date() constructor. There are 4 ways of initiating a date:

Syntax:

new Date()
new Date(milliseconds)
new Date(dateString)
new Date(year, month, day, hours, minutes, seconds, milliseconds)

JavaScript Date

In this example, first a date object is created, and then all the information about the current date and time in that object, gets displayed on the screen.


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> JavaScript Date Object </title>
</head>
<body>
Current Date and Time: <span id="demo"></span>
<script>
var today=new Date();
document.getElementById('demo').innerHTML=today;
</script>
</body>
</html>

Output

JavaScript Date Object Current Date and Time:

JavaScript Current Time

It is same as the previous one but here, the single digit in date and time will be added with zero before them. It looks good.


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> JavaScript Current Date Method </title>
</head>
<body>
<h3>Current Time: </h3>
<span id="date"></span>
<script>
var today=new Date();
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();
document.getElementById('date').innerHTML=h+":"+m+":"+s;
</script>
</body>
</html>

Output

JavaScript Current Date Method

Current Time:


JavaScript Current Date and Time

The JavaScript Date object can provide dates in different formats by using methods like toDateString(), toLocaleDateString(), etc. Look at the example below to understand it better :-


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> JavaScript Live Digital Clock </title>
</head>
<body>
Current Date and Time:<br />
<span id="digital-clock"></span>
<script>
window.onload=function(){getTime();}
function getTime(){
var today=new Date();
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();
// add a zero in front of numbers<10
m=checkTime(m);
s=checkTime(s);
document.getElementById('digital-clock').innerHTML=today;
setTimeout(function(){getTime()},1000);
}
//setInterval("getTime()",1000);//another way
function checkTime(i){
if (i<10){
i="0" + i;
}
return i;
}
</script>
</body>
</html>

Output

JavaScript Live Digital Clock Current Date and Time:

Date and Time Strings

The JavaScript Date object can provide dates in diferent formats by using methods like toLocaleDateString(), toDateString() etc.

Look at the example below to understand it better :


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> JavaScript Generate Date String </title>
</head>
<body>
<script>
var d = new Date();
document.write(d.toDateString() + "<br>");
document.write(d.toLocaleDateString() + "<br>");
document.write(d.toISOString() + "<br>");
document.write(d.toUTCString() + "<br>");
document.write(d.toString());
</script>
</body>
</html>

Output

JavaScript Generate Date String

Time in Hours, Minutes, Seconds, and Milliseconds

The Date object also provide some methods to extract time components individually like getHours(), getMinutes(), getSeconds(), etc.


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> JavaScript Date </title>
</head>
<body>
<script>
var date = new Date();
// Extracting time part
document.write("Hours : " + date.getHours() + "<br>");
document.write("Minutes : " + date.getMinutes() + "<br>");
document.write("Seconds : " + date.getSeconds() + "<br>");
document.write("Miliseconds : " + date.getMilliseconds() + "<br>");
document.write("Time : " + date.getTime() + "<br>");
document.write("Time Zone : " + date.getTimezoneOffset());
</script>
</body>
</html>

Output

JavaScript Date

Note : The getTime() method returns the number of milliseconds between midnight of January 1, 1970 and the specified date.











Follow Us: