最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to calculate number of days between two dates in javascript - Stack Overflow

programmeradmin4浏览0评论

I am having two dated in dd/mm/yyyy format. How to calculate the number of days between these two dates in javascript/jquery.

Example: Fom date is 20/06/2000, to date is 16/08/2011

I am having two dated in dd/mm/yyyy format. How to calculate the number of days between these two dates in javascript/jquery.

Example: Fom date is 20/06/2000, to date is 16/08/2011

Share Improve this question asked Aug 16, 2011 at 8:25 vinothinivinothini 2,6044 gold badges29 silver badges42 bronze badges 4
  • 2 possible duplicate: stackoverflow./questions/327429/… – LeeR Commented Aug 16, 2011 at 8:27
  • 2 See stackoverflow./questions/1410285/… – Reporter Commented Aug 16, 2011 at 8:27
  • 1 @vinothini You will need to accept more answers before asking any more questions. – Ariel Commented Aug 16, 2011 at 8:28
  • See related section of this question page. You will find similar questions. – Harry Joy Commented Aug 16, 2011 at 8:29
Add a ment  | 

3 Answers 3

Reset to default 8

Simple code

var Date1 = new Date (2008, 7, 25);
var Date2 = new Date (2009, 0, 12);
var Days = Math.round((Date2.getTime() - Date1.getTime())/(1000*60*60*24));
var date1 = new Date(2000, 6, 20);
var date2 = new Date(2011, 8, 16);

var one_day = 1000*60*60*24; //Get 1 day in milliseconds

var days = Math.ceil( (date2.getTime() - date1.getTime() ) / one_day);

Math.ceil to round up, Math.floor to round down.

http://www.javascriptkit./javatutors/datedifference.shtml

t1="10/10/2006";
t2="15/10/2006";

//Total time for one day
var one_day=1000*60*60*24;  //Here we need to split the inputed dates to convert them into standard format for further execution
var x=t1.split("/");     
var y=t2.split("/");   //date format(Fullyear,month,date) 

var date1=new Date(x[2],(x[1]-1),x[0]);

// it is not coded by me,but it works correctly,it may be useful to all

var date2=new Date(y[2],(y[1]-1),y[0])
var month1=x[1]-1;
var month2=y[1]-1;

//Calculate difference between the two dates, and convert to days

_Diff=Math.ceil((date2.getTime()-date1.getTime())/(one_day));
发布评论

评论列表(0)

  1. 暂无评论