JavaScript Date Object
You employ the JavaScript date object simply to determine days, months, and years. The JavaScript date object also has the ability to be used to display a timer on a webpage.
The Date object is a special datatype that is incorporated into the JavaScript programming language. The new Date() method is used to construct data objects, as demonstrated in the Syntax below.
Once the Date object is generated, you can use a variety of methods to manipulate it. Most methods merely enable you to retrieve and set the object's day, year, month, millisecond, second, minute, and hour fields in either local or UTC (universal, or GMT) time.
To generate a date object, you can utilize various Date constructors. It includes methods for retrieving and setting the second, minute, hour, day, month, and year as mentioned above.
Javascript Date Constructors / Syntax
To build a date object, you can use one of four Date constructor variants.
- Date()
<script> // "hubtolearn" is Date object var hubtolearn = new Date(); document.write(hubtolearn); // Prints todays date </script>
- Date(dataString)
<script> // "hubtolearn" is Date object var hubtolearn = new Date("August 18, 2022 10:39:54"); document.write("Datastring with day : " + hubtolearn); </script>
- Date(milliseconds)
<script> // "hubtolearn" is Date object var hubtolearn = new Date(4500); document.write("Todays date : " + hubtolearn); </script>
- Date (seconds, minutes, milliseconds, hours, days, months, years)
Description of Parameters and Arguments
- Date (dataString) is a string representation of a date in the format supported by the Date.parse() method whenever one string argument is given.
- Without arguments, the Date() constructor returns a Date object with the current time and date.
- Millisecond: This is an Integer Value depicting a millisecond segment in a time reading.
- Seconds: This is an Integer Value depicting a second segment in a time reading.
- Minute: This is an Integer Value depicting a minute segment in a time reading.
- Hours: This is an Integer Value depicting an hourly segment in a time reading.
- Months: This is an Integer Value depicting a monthly segment in a time reading. It depicts January as Zero (0) and December as Eleven(11).
- Years: This is an Integer Value depicting a yearly segment in a time reading. Always write the year in full. For example, write it out as 1999 instead of 99.'
Autocorrection
Date objects' autocorrection is a highly useful feature. We can enter values that are out of range, and it will automatically adapt.
As an example:
let date = new Date(2022, 1, 18); // 18 February 2022 ?!? alert(date); // ...is 18th Mar 2022!
Date components that are out of range are dispersed automatically.
Assume we need to add two days to the date "28 February 2022." In the event of a leap year, it may be "1 Mar" or "2 Mar." We don't need to consider it. Simply add two days. The Date object will take care of the rest:
let date = new Date(2022, 1, 28); date.setDate(date.getDate() + 2); alert( date ); // 1 Mar 2022
This function is frequently used to obtain the date after a certain time period. Take, for example, the date "80 seconds from now":
let date = new Date(); date.setSeconds(date.getSeconds() + 80); alert( date ); // will show the correct date
We can even specify zero or negative numbers. As an example:
let date = new Date(2022, 0, 2); // 2 Jan 2022 date.setDate(1); // set day 1 of month alert( date ); date.setDate(0); // min day is 1, and this means that the last day in the previous month will be assumed alert( date ); // 31 Dec 2021
Example of a Digital Clock using Javascript
Let's look at a simple instance of displaying a digital clock with a JavaScript date object.
In JavaScript, you may set intervals using either the setInterval() or setTimeout() methods.
Current Time: <span id="txt"></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('txt').innerHTML=h+":"+m+":"+s; setTimeout(function(){getTime()},1000); } //setInterval("getTime()",1000);//another way function checkTime(i){ if (i<10){ i="0" + i; } return i; } </script>