Skip to content Skip to sidebar Skip to footer

Finding Difference Between Two Dates Times In Javascript

Problem Statement : I have to validate a form having fields 'Start Date' and 'End Date'. Validation Constraint : Difference between these two dates must not be less than 24 hours

Solution 1:

Once you have parsed the 2 strings and created the corresponding Date object, you only have to use :

function isValid(startDate, endDate) {
  return endDate - startDate > 24 * 60 * 60 * 1000;
}

Solution 2:

Have a look at moment.js. It almost certainly does what you want.


Solution 3:

You can use this to calculate the difference and perform your validation:

 Date.daysBetween = function( date1, date2 ) {
  //Get 1 day in milliseconds
  var one_day=1000*60*60*24;

  // Convert both dates to milliseconds
  var date1_ms = date1.getTime();
  var date2_ms = date2.getTime();

  // Calculate the difference in milliseconds
  var difference_ms = date2_ms - date1_ms;
  //take out milliseconds
  difference_ms = difference_ms/1000;
  var seconds = Math.floor(difference_ms % 60);
  difference_ms = difference_ms/60; 
  var minutes = Math.floor(difference_ms % 60);
  difference_ms = difference_ms/60; 
  var hours = Math.floor(difference_ms % 24);  
  var days = Math.floor(difference_ms/24);

  return days + ' days, ' + hours + ' hours, ' + minutes + ' minutes, and ' + seconds + ' seconds';
}

Solution 4:

The hard part is converting the strings into valid Date objects, particularly since you are using a non-standard date format.

I would advise using a third party library which can cope with that format, or use simple string manipulation and use the version of the Data constructor that takes six individual fields for each of (year, month, date, hour, minute, seconds).

Having done that, testing whether two JavaScript Date objects are more than 24 hours apart is the easy bit - just take their numeric value and check whether it's more than 86400000 (the number of milliseconds in a day).

var t1 = new Date(Y1, M1, D1, hh1, mm1, ss1);
var t2 = new Date(Y2, M2, D2, hh2, mm2, ss2);

var valid = (t2 - t1) >= 86400000;

Solution 5:

  /** Method: getDateObject
   * 
   * Description: 
   * gives date object from string 
   * @param dateString
   * @param separator used
   * @returns corresponding date object 
   */

function getDateObject(dateString, parseFormat) {
//This function return a date object after accepting

 if(dateString!=null && dateString!=undefined && dateString!="")
 {
  var dateFormat = sessvars.currentUser.options.dateFormat;
  if(parseFormat.length==0)
      parseFormat = [dateFormat + " HH:mm:ss", dateFormat + " HH:mm",dateFormat];

  var dtObject = Date.parseExact(dateString, parseFormat);  
  return dtObject;
 }
else
  return dateString;
}

Ok Thanx a lot guys for your valuable advice and suggestions. I have got a solution using a mix of your ideas.

Answer :

1.Made a custom function which return date object according to the format specified.

2.Now getTime() which gives milliseconds value.

3.SDate.getTime() - EDate.getTime() < 24*3600*1000

Example :

getDateObject("01-10-2012 05:30",[]).getTime() - getDateObject("02-10-2012 05:30",[]).getTime() < 24*3600*1000


Post a Comment for "Finding Difference Between Two Dates Times In Javascript"