JavaScript uses Date to implement a simple countdown instance.
Introduction
Date object, which is the object that operates on the Date and time. The Date object can only operate on the Date and time through methods. Similar to Array in js, Date is a special object with its own special method.
Syntax for creating a Date object:
Var myDate = new Date () // The Date object automatically saves the current Date and time as its initial value.
Before getting the countdown, let's take a look at how to get the current time !!!
Function time () {var oDate = new Date (); var year = oDate. getFullYear (); var month = oDate. getMonth () + 1; var dDate = oDate. getDate (); var day = oDate. getDay (); var house = oDate. getHours (); var minu = oDate. getMinutes (); var sec = oDate. getSeconds (); switch (day) {case 1: day = 'monday'; break; case 2: day = 'tuesday'; break; case 3: day = 'weday '; break; case 4: day = 'thurs'; break; case 5: day = 'Friday'; break; case 6: day = 'satur'; break; case 0: day = 'sunday'; break;} function double (t) {if (t <10) {t = '0' + t;} return t;} document. body. innerHTML = year + 'Year' + month + 'month' + dDate + 'day' + day + ''+ double (house) + ':' + double (minu) + ':' + double (sec);} time (); setInterval (time, 1000 );
Get countdown
1. Implement the countdown Function
2. The countdown end button can be clicked and cannot be clicked before.
Instance code:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
As follows:
Summary
The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.