How To Find Minimum Date From 3 Dates Using Javascript Function?
I want to find minimum date from 3 dates (8/1/2011,6/1/2011,7/1/2011) format is (mm/dd/yyyy) using java script.
Solution 1:
Convert the dates to int, use Math.min to find the smallest:
var dates = ['8/1/2011', '6/1/2011', '7/1/2011']; // dates
dates = dates.map(function(d) {
d=d.split('/');
returnnewDate(d[2], parseInt(d[0])-1, d[1]).getTime();
}); // convertvar minDate = newDate(Math.min.apply(null, dates)); // smallest > Date
Solution 2:
Untested code, but you should get the idea!
// Initialise an array of dates, with correct values// You want 3 dates, so put them in herevarMyDates= NewArray('15/10/2000','28/05/1999','17/09/2005');
// A function that takes an array of dates as its input// and returns the smallest (earliest) date// MUST take at LEAST 2 dates or will throw an error!functionGetSmallestDate(DateArray){
varSmallestDate = newDate(DateArray[0]);
for(var i = 1; i < DateArray.length; i++)
{
varTempDate = newDate(DateArray[i]);
if(TempDate < SmallestDate)
SmallestDate = TempDate ;
}
returnSmallestDate ;
}
// Call the function!alert(GetSmallestDate(MyDates));
Solution 3:
/**
* Return the timestamp of a MM/DD/YYYY date
*/functiongetTime(date) {
var tmp = date.split('/');
var d = newDate(tmp[2], parseInt(tmp[0])-1, tmp[1]);
return d.getTime();
}
// then return the output: getTime('6/7/2000') < getTime('6/7/2001')
See Date for details. If you are a jQuery UI user, you might find their date parsing method helpful.
Post a Comment for "How To Find Minimum Date From 3 Dates Using Javascript Function?"