JavaScript get today's date

 Use Date() in JavaScript to get the time information, and then you can get the results of the day, month, and year through several commands such as getDate(), getMonth(), getFullYear(). These commands have been built in In the browser, you can use it as long as you call it out. Please see the following simple example.


JavaScript get today's date example
<script language="javascript">
 var Today=new Date();
 document.write("Today's date is" + Today.getFullYear()+ "year" + (Today.getMonth()+1) + "month" + Today.getDate() + "day");
</script>
Present results

Today's date is August 18, 2020


In the example, var Today=new Date(); means to use Today to represent the object of new Date(), Today can be written in other words to represent, and new Date() is the time record of the netizen entering the webpage, which is represented by Today new Date() object, then you can use commands to call out the year, month, and day, document.write is to let the browser display the content, getFullYear() is to display the year, getMonth() is to display the month, and getDate() is The day is displayed. The special thing is that getMonth() will start from zero by default, so we add him +1 to correct the result. JavaScript also has several time-related commands such as getHour(), getMinute(), and getSecond(), which represent hours, minutes, and seconds respectively. You can also add them to the example to see how the results are.

The time shown in this example is the time when the netizen entered the webpage. If the netizen does not re-enter the webpage, the time will not change. To display the current time on the webpage, additional writing is required. We will introduce it in another space.

Post a Comment

0 Comments