JavaScript uses Date to implement a simple countdown instance.

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.