如何比较从另一个日期中减去一个日期并获得日期?

时间:2013-02-18 12:22:05

标签: javascript datetime

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

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

4 个答案:

答案 0 :(得分:7)

注意:此答案假设STRING为mm/dd/yyyy
如果您在Chrome中使用type="date",则会收到yyyy-dd-mm

的字符串

DEMO

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);   

答案 1 :(得分:2)

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

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

答案 2 :(得分:1)

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

注意:我已经将逻辑分发到多种方法中,可以在许多不同的场景中使用(作为帮助者)。

/** 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);

答案 3 :(得分:-2)

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