Example code that JavaScript determines the difference between two dates

Source: Internet
Author: User
Tags date1

Example code that JavaScript determines the difference between two dates

<!--newszw_hzh_begin-->
Date difference We need to use the same as in PHP, such as 2015-08-30, and then after the conversion to seconds after the two-day number of seconds to subtract after the addition of judgment on it, if the judgment date is more simple than the example at the end of the article to illustrate.

Example 1, date difference function

function Better_time (strdatestart,strdateend) {var strseparator = "-";//Date delimiter var Strdatearraystart;  var strdatearrayend;  var intday;  Strdatearraystart = Strdatestart.split (strseparator);  Strdatearrayend = Strdateend.split (strseparator);  var strdates = new Date (strdatearraystart[0] + "/" + strdatearraystart[1] + "/" + strdatearraystart[2]);  var strdatee = new Date (strdatearrayend[0] + "/" + strdatearrayend[1] + "/" + strdatearrayend[2]);  Intday = (strdatee-strdates)/(1000*3600*24); return intday; }



Example 2

Function checktime () {   var dateinp=$ ("#dateInp"). Val ();   var  Day1=date.parse (Dateinp.replace (/-/g,  "/"));    var nowdate = new date ( );    var datestr = nowdate.getfullyear () + "/" + (Nowdate.getmonth ()  + 1) + "/" +nowdate.getdate ();            var day2= Date.parse (DATESTR);    var aparttime=day1-day2;   var apartday= parseint (aparttime /  (1000 * 60 * 60 * 24));    if ( apartday ==0) {     alert ("No Appointment on day");     return  false;   }else if  (apartday < 1 | |  apartday > 3) {     alert ("Appointment date out of range");      return false;   }  }



Judge dates Equal

var date1 = new Date ("2013-11-29"); var date2 = new Date ("2013-11-29"); Console.log (date1.gettime () = = Date2.gettime ()); True



Watch out, people, don't write like this!

var date1 = new Date ("2013-11-29"); var date2 = new Date ("2013-11-29"); Console.log (date1 = = Date2); False



This is wrong, because the date is the object after using the new date, so the object cannot be compared as a character.  

Reference Source:
Example code that JavaScript determines the difference between two dates
Http://www.lai18.com/content/439055.html

Example code that JavaScript determines the difference between two dates

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.