The JavaScript Date object allows you to work with dates and times. You can use it to get the current date and time, manipulate dates, and format them for display. Here’s a brief overview of the Date object and its constructors:
You can create a Date object using one of the following constructors:
<script>
var now = new Date();
document.write(now);
</script>
Creates a Date object representing the date and time at the specified number of milliseconds since January 1, 1970, 00:00:00 UTC.
<script>
var dateFromMilliseconds = new Date(1000000000000); // Example milliseconds
document.write(dateFromMilliseconds);
</script>
Creates a Date object representing the date and time specified by the date string. The format of the date string can vary, but it must be recognized by the Date.parse() method.
<script>
var dateFromString = new Date("August 2, 2024 12:00:00");
document.write(dateFromString);
</script>
Creates a Date object representing the date and time specified by the individual date and time components. Note that the month is zero-indexed (0 for January, 11 for December).
<script>
var specificDate = new Date(2024, 7, 2, 12, 0, 0); // August 2, 2024 12:00:00
document.write(specificDate);
</script>
Here is a complete HTML example showing how to use the Date object constructors:
<script>
var now = new Date();
document.write("Current Date and Time: " + now + "<br>");
var dateFromMilliseconds = new Date(1000000000000);
document.write("Date from milliseconds: " + dateFromMilliseconds + "<br>");
var dateFromString = new Date("August 2, 2024 12:00:00");
document.write("Date from string: " + dateFromString + "<br>");
var specificDate = new Date(2024, 7, 2, 12, 0, 0);
document.write("Specific Date: " + specificDate + "<br>");
</script>
Current Date and Time: Fri Aug 02 2024 12:26:21 GMT+0530 (India Standard Time)
Date from milliseconds: Sun Sep 09 2001 07:16:40 GMT+0530 (India Standard Time)
Date from string: Fri Aug 02 2024 12:00:00 GMT+0530 (India Standard Time)
Specific Date: Fri Aug 02 2024 12:00:00 GMT+0530 (India Standard Time)