JavaScript method for date difference _javascript tips

Source: Internet
Author: User

The examples in this article describe the way JavaScript asks for a date difference. Share to everyone for your reference, specific as follows:

<script type= "Text/javascript" >
function Daytonow (year, month, date) {
 //The idea is to convert two dates to the time stamp, which is the number of milliseconds, Divided by the number of milliseconds per day. How many days apart
 //js month is from 0, so month to reduce a
 month--;
 Past days
 var tdate = new Date (year, month, date). GetTime ();
 Today
 var tnow = new Date (). GetTime ();
 var longdate = Math.ceil (tnow-tdate)/(1000 * *));
 return longdate;
}
Alert (Daytonow (2009, 4, 5));
</script>

Difference between two date days:

 number of days of the date string, before-after, Sdate1-sdate2 function DateDiff (sDate1, SDate2) {//sdate1 and SDate2 are "2002
  -12-18 "format var adate, oDate1, ODate2, idays;"
  Adate = Sdate1.split ("-");
  oDate1 = new Date (adate[0], adate[1]-1, adate[2]);
  Adate = Sdate2.split ("-");
  ODate2 = new Date (adate[0], adate[1]-1, adate[2]);
  Idays = parseint (Math.Abs (odate1-odate2)/1000/60/60/24);
  if ((Odate1-odate2) < 0) {return-idays;
return idays; }//Two date string of days of difference, before-after, Sdate1-sdate2 function DateDiff2 (sDate1, SDate2) {//sdate1 and SDate2 are "12/18/2011" format var oDate1, ODate2
  , Idays;
  oDate1 = new Date (sDate1);
  ODate2 = new Date (SDATE2);
  var idays = parseint (Math.Abs (odate1-odate2)/1000/60/60/24);
  if ((Odate1-odate2) < 0) {return-idays;
return idays; }

More readers interested in JavaScript-related content can view the site topics: "JavaScript Search Algorithm Skills Summary", "JavaScript animation effects and Skills summary", "JavaScript Error and debugging skills summary", " JavaScript data structure and algorithm skills summary, "javascript traversal algorithm and Skills summary" and "JavaScript Mathematical Computing Usage Summary"

I hope this article will help you with JavaScript programming.

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.