JS Tutorial
JS Version
JS Objects
JS Function
JS Classes
JS Async
JS HTML DOM
JS Browser BOM
JS Web API
JS AJAX
JS JSON
JS vs JQUERY
JS Graphics
JavaScript Date Objects
JavaScript Date Output
By default, JavaScript uses the browser’s time zone and shows a date as a full text string:
Thu Jun 02 2022 00:49:53 GMT+0530 (India Standard Time)
Creating Date Objects
The new Date() constructor creates data objects .
There are 4 ways to create a new date object:
Example
new Date()
new Date(year, month, day, hours, minutes, seconds, milliseconds)
new Date(milliseconds)
new Date(date string)
new Date()
new Date() creates a new date object using the current date and time:
Example
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript new Date()</h2>
<p>Using new Date(), creates a new date object with the current date and time:</p>
<p id=”demo”></p>
<script>
const d = new Date();
document.getElementById(“demo”).innerHTML = d;
</script>
</body>
</html>
Output
JavaScript new Date()
Using new Date(), creates a new date object with the current date and time:
new Date(year, month, …)
new Date(year, month, …) generates a new date object with a specified date and time.
7 numbers defines year, month, day, hour, minute, second, and millisecond (in that order):
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript new Date()</h2>
<p>Using new Date(7 numbers), creates a new date object with the specified date and time:</p>
<p id=”demo”></p>
<script>
const d = new Date(2018, 11, 24, 10, 33, 30, 0);
document.getElementById(“demo”).innerHTML = d;
</script>
</body>
</html>
Output
JavaScript new Date()
Using new Date(7 numbers), creates a new date object with the specified date and time:
Mon Dec 24 2018 10:33:30 GMT+0530 (India Standard Time)new Date(dateString)
new Date(dateString) creates a new date object from a date string:
Example
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript new Date()</h2>
<p>A Date object can be created with a specified date and time:</p>
<p id=”demo”></p>
<script>
const d = new Date(“October 13, 2014 11:13:00”);
document.getElementById(“demo”).innerHTML = d;
</script>
</body>
</html>
Output
JavaScript new Date()
A Date object can be created with a specified date and time:
Mon Oct 13 2014 11:13:00 GMT+0530 (India Standard Time)new Date(milliseconds)
new Date(milliseconds) generates a new date object as zero time plus milliseconds:
Example
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript new Date()</h2>
<p>Using new Date(milliseconds), creates a new date object as January 1, 1970, 00:00:00 Universal Time (UTC) plus the milliseconds:</p>
<p id=”demo”></p>
<script>
const d = new Date(0);
document.getElementById(“demo”).innerHTML = d;
</script>
</body>
</html>
Output
JavaScript new Date()
Using new Date(milliseconds), creates a new date object as January 1, 1970, 00:00:00 Universal Time (UTC) plus the milliseconds:
Thu Jan 01 1970 05:30:00 GMT+0530 (India Standard Time