如何比较从一个日期减去另一个日期并获得天数


function createDate(d) {
   var year = parseFloat(d.substring(6, 10)); 
   var month = parseFloat(d.substring(0,2)); 
   var day = parseFloat(d.substring(3, 5)); 
   var d = new Date(year, month, day); 
   return d; 
}
var d1 = $("#DateCarArrival").val();
var d2 = $("#DateCarLeaving").val(); 
var date1 = createDate(d1); 
var date2 = createDate(d2);
console.log(date1.toString()); 
console.log(date2.toString());   
//       Mon Feb 18 2013 13:19:26 GMT+0100
//      Mon Feb 18 2013 13:19:26 GMT+0100

如何比较这些日期并获得它们之间的天数?

注意:此答案假设字符串为mm/dd/yyyy
如果你在Chrome中使用type="date",你会得到yyyy-dd-mm 的字符串

演示

function createDate(d) {
   var year = parseInt(d.substring(6, 10),10); 
   var month = parseInt(d.substring(0,2),10); 
   var day = parseInt(d.substring(3, 5),10); 
   var d = new Date(year, month-1, day); // JS Months are 0 based
   return d; 
}
var d1 = $("#DateCarArrival").val();
var d2 = $("#DateCarLeaving").val(); 
var date1 = createDate(d1); 
var date2 = createDate(d2);
var aDay = 24*60*60*1000;
var diff = Math.abs((date1.getTime()-date2.getTime())/aDay)
console.log(date1.toString(),date2.toString(),diff);   

将日期转换为毫秒(使用valueOf()(,然后减去

DifferNumDays = (date1.valueOf() - date2.valueOf())/(24*60*60*1000);

要获得差异(以天为单位(并比较两个日期,可以使用以下原型扩展器方法:

注意:我已经将逻辑分布到多个方法中,这些方法可以在许多不同的场景中使用(作为助手(

/** Gets the type (name) of the specified object. 
 */
if (!Object.prototype.getType) {
    Object.prototype.getType = function (obj) {
        return ({}).toString.call(obj).match(/s([a-z|A-Z]+)/)[1].toLowerCase();
    }
}
/** Checks whether the object is Date. 
 */
if (!Object.prototype.isDate) {
    Object.prototype.isDate = function (date) {
        return Object.getType(date) === 'date';
    }
}
/** Gets the difference in days, between the given dates. 
 *  If an invalid date is passed as a parameter, returns null.
 */
if (!Date.prototype.difference) {
    Date.prototype.difference = function (date1, date2) {
        if (!(Object.isDate(date1) && Object.isDate(date2))) return null;
        var diff = Math.abs(date1.getTime() - date2.getTime()),
            msInOneDay = 1000 * 60 * 60 * 24;
        return Math.round(diff / msInOneDay);
    }
}
/** Compares the date instance to the specified date value and returns an integer that indicates whether 
 *  the instance is earlier than, the same as, or later than the specified date value. 
 *  @return -1 if the specified date is invalid. 0 if they are the same. 1 if date1 is greater. 2 if date2 is greater.
 */
if (!Date.prototype.compareTo) {
    Date.prototype.compareTo = function (date) {
        if (!Object.isDate(date)) return -1; //not a Date
        if (this == date) {
            return 0;
        } else if (this > date) {
            return 1;
        } else {
            return 2; //date > this
        } 
    }
}

用法:

//Validate a date object
var dateIsValid = Object.isDate(date1);
//get the difference in days
var numOfDaysBetween = Date.difference(date1, date2);
//compare date to another
var comparison = date1.compareTo(date2);

您可以使用getMilliseconds((将日期转换为毫秒,减去它们并从毫秒中计数。

相关内容

  • 没有找到相关文章

最新更新