Reputation: 417
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.
Example: Invalid case : SDate : "01-10-2012 11:59" EDate : ""02-10-2012 00:00"
SDate : "01-10-2012 02:20" EDate : ""02-10-2012 02:00"
Valid Case :
SDate : "01-10-2012 02:20" EDate : ""02-10-2012 03:30"
Note : These two date objects have time value also.
Upvotes: 1
Views: 4481
Reputation: 2463
function(start, end, unit){
var denom = 1,
diff = end.getTime() - start.getTime();
if(unit == 's'){
denom = 1000;
}
else if(unit == 'm'){
denom = 1000*60;
}
else if(unit == 'h'){
denom = 1000*60*60;
}
return Math.round(diff/denom);
}
unit can be 's' - seconds, 'm' - minutes, 'h' - days. You can extend this for your choice: days, months ect.
You should also parse your dates to javascript Date
object first. Do you need help with that?
Date parsing from your format:
var p = "01-10-2012 05:30".match(/(\d{2})-(\d{2})-(\d{4}) (\d{2}):(\d{2})/);
parsedDate = new Date(parseInt(p[3]) - 1900, parseInt(p[2]) - 1, p[1], p[4], p[5]);
Upvotes: 0
Reputation: 417
/** 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.
Solution:
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
Upvotes: 0
Reputation: 76183
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;
}
Upvotes: 1
Reputation: 339786
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;
Upvotes: 0
Reputation: 3435
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';
}
Upvotes: 0
Reputation: 109232
Have a look at moment.js. It almost certainly does what you want.
Upvotes: 1