JavaScript - find months between two dates?

Asked By >>ohm<< o
16-Aug-11 01:32 AM
how to find out moths between two dates?

using javascript
  Web Star replied to >>ohm<< o
16-Aug-11 01:39 AM
you can use this javascript function for that
function monthDiff(d1, d2) {
   
var months;
    months
= (d2.getFullYear() - d1.getFullYear()) * 12;
    months
-= d1.getMonth() + 1;
    months
+= d2.getMonth();
   
return months;
}
  Riley K replied to >>ohm<< o
16-Aug-11 01:41 AM


Use this Function 

function countMonths ( startDate:Date, endDate:Date ):int
  {
    var stepDate:Date = new Date;
    stepDate.time = startDate.time;
    var monthCount:int;
 
    while( stepDate.time <= endDate.time ) {
      stepDate.month += 1;
      monthCount += 1;
    }      
 
    if ( stepDate != endDate ) {
      monthCount -= 1;
    }
 
    return monthCount;
  }

Regards
  Web Star replied to >>ohm<< o
16-Aug-11 01:44 AM
Also you can calculate month between two dates as follows
function countMonths ( startDate:Date, endDate:Date ):int
   
{
       
var stepDate:Date = new Date;
        stepDate
.time = startDate.time;
       
var monthCount:int;

       
while( stepDate.time <= endDate.time ) {
            stepDate
.month += 1;
            monthCount
+= 1;
       
}          

       
if ( stepDate != endDate ) {
            monthCount
-= 1;
       
}

       
return monthCount;
   
}
  James H replied to >>ohm<< o
16-Aug-11 03:00 AM

If you don't care about the time component, you can use .getDate() and .setDate() to just set the date part.

So to set your end date to 2 weeks after your start date, do something like this:

function GetEndDate(startDate)
{
  var endDate = new Date(startDate.getTime());
  endDate.setDate(endDate.getDate()+14);
  return endDate;
}

To return the difference (in days) between two dates, do this:

function GetDateDiff(startDate, endDate)
{
  return endDate.getDate() - startDate.getDate();
}

Finally, let's modify the first function so it can take the value returned by 2nd as a parameter:

function GetEndDate(startDate, days)
{
  var endDate = new Date(startDate.getTime());
  endDate.setDate(endDate.getDate() + days);
  return endDate;
}
  Irfan Khan replied to >>ohm<< o
16-Aug-11 03:24 AM

var a = someDate;

var b = someOtherDate;

// Months between years.

var months = (b.getFullYear() - a.getFullYear()) * 12;

// Months between... months.

months += b.getMonth() - a.getMonth();

// Subtract one month if b's date is less that a's.

if (b.getDate() < a.getDate())

{

    months--;

}

  Vickey F replied to >>ohm<< o
16-Aug-11 10:12 AM
Use this Javascript code-

Date.monthsDiff=
function(day1,day2)
{
var d1= day1,d2= day2;
if(day1<day2){
d1= day2;
d2= day1;
}

var m= (d1.getFullYear()-d2.getFullYear())*12+(d1.getMonth()-d2.getMonth());
if(d1.getDate()<d2.getDate()) --m;
return m;
}
//test case

var d1= new Date(2007,9,12);
var d2= new Date(2006,8,13);

alert(Date.monthsDiff(d1,d2));

Try this and let me know.
  Radhika roy replied to >>ohm<< o
16-Aug-11 10:17 AM

 

 Use this function-

function
countMonths ( startDate:Date,
endDate:Date ):int
   
{
       
var stepDate:Date = new Date;
        stepDate
.time = startDate.time;
       
var monthCount:int;

       
while( stepDate.time <= endDate.time ) {
            stepDate
.month += 1;
            monthCount
+= 1;
       
}          

       
if ( stepDate != endDate ) {
            monthCount
-= 1;
       
}

       
return monthCount;
   
}


Hope this will help you.

 

 

Create New Account
help
how to subtract one date from other date hi guys iam iam in a project where i have two text boxes i.e we have to select the date from date to TO date and i have a button if click the button the differences of two days should a label The easy solution is to use the TimeSpan class and its members. DateTime.Subtract method returns a TimeSpan object, which has properties such as Days, Hours, Minutes and so on to get the difference in specific measurements. Here is code snippet: / / Start date DateTime startDate = new DateTime(textBox1.Text ); / / End date DateTime endDate = new DateTime( textBox2.Text ); / / Time
writing code, but program in queries regularly. Have query that includes two dates. Need to subtract date 1 from date 2 and get response in ##years ## months ## days. Example 10 / 03 / 1984-07 / 06 / 1954 result of 30 However david dob and mom dob are fields that already includes the date and I am trying to avoid entering the date again in a pop up box as well as trying to get the true result searching through past messages here. Thanks, for any help. Access Queries Discussions Microsoft Access (1) EndDate (1) IntHold (1) Outlook (1) StartDate (1) DayHold (1) DateDiff (1) MVP (1) See the standard module, then call it from the debug window as shown: Function fAgeYMD(StartDate As Date, EndDate As Date) As String 'Purpose: Returns the difference between StartDate and EndDate in full years
you can difference between two dates in number of days, hour, minutes, seconds, milliseconds. / / starting date. . assign any date DateTime startdate = DateTime . Now ; / / End date . . assign any date DateTime enddate = DateTime . Now . AddDays (10); / / Substract datetime returns Time span TimeSpan diff = enddate. Subtract (startdate); MessageBox. Show ( "Total Days " + diff. Days .ToString()); MessageBox. Show ( "Total Hours " + diff. Hours .ToString dates ( 173 Views ) View karan patel's FAQs Create New Account keywords: DateTime, AddDays, TimeSpan, Subtract, Show, Hours, Minutes, Seconds, Milliseconds description: Here i wil show how you can difference between
Calculating Date Difference I am trying to insert my date diff (which is days or hour) in to my datebase I have two filed. . .Start date and End date. and End Date > = Start Date so which will show me the number of days and hours. Dim SQL As String As DateTime = AddCurrentTime(DateTime.Parse(txtEndDate.Text)) xy.Append( " INSERT INTO temp" ) xy.Append( " (startdate, enddate, overlap)" ) xy.Append( " VALUES" ) xy.Append( " ('{0}', '{1}', '{2}')" ) SQL = String .Format(xy.ToString, dtStartDate yy hh:mm:ss tt" ).ToUpper(), Me .txtDatediff.Text) Please, help me to calculate the date difference. HI, Find the below sample code Private Sub DetermineNumberofDays ( ) Dim dtStartDate As Date = "1