JS calculates the incorrect number of days for the two date and time difference

Source: Internet
Author: User

Do a time countdown and find that the days are always incorrect.

JS Code:

1 //Define target Date2 varTargettime =NewDate ();3 4 //Target Date5Targettime.setfullyear (2015,5,1);6 7 //Target when8Targettime.sethours (12);9 Ten //Target points OneTargettime.setminutes (0); A  - //Target seconds -Targettime.setseconds (0); the  - //Target milliseconds -Targettime.setmilliseconds (0); -  + //converted to milliseconds - varEndTime =Targettime.gettime ();  +  A //Defining Methods at functionGetcountdowntime () { -         -             //define the date and time that the parameter returns today -             varNowtime =NewDate (); -              -             //Time Millisecond Difference in             varCOUNTDOWNMS = EndTime-nowtime.gettime (); -              to             //Get the time difference +             varCountdowndays = Math.floor (Countdownms/(1000 * 60 * 60 * 24 )); -  the             //To arrive at a time-hour difference *             varCountdownh = Math.floor (Countdownms/(1000 * 60 * 60))% 24; $ Panax Notoginseng             //To arrive at a time minute difference -             varCOUNTDOWNM = Math.floor (Countdownms/(1000 * 60))% 60; the  +             //To get a time difference of seconds A             varcountdowns = Math.floor (countdownms/1000)% 60; the             } +         }  -  $  $$ (document). Ready (function () { -Window.setinterval ("Getcountdowntime ()", 1000); -});

In the calculated number of days, the countdown is always different from the defined target date by about 30 days

// target date targettime.setfullyear (2015,5,1);

Here's the problem:

The three parameter of the setFullYear () method is Year,month,day, where month represents the number of months between 0~11.

So the target date of May 1, 2015 should be

// target date targettime.setfullyear (2015,4,1);

JS calculates the incorrect number of days for the two date and time difference

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.