admin管理员组文章数量:1410712
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
How to pare these dates and get the number of days they are in between?
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
How to pare these dates and get the number of days they are in between?
Share edited Feb 18, 2013 at 12:30 Marko asked Feb 18, 2013 at 12:22 MarkoMarko 11k17 gold badges73 silver badges93 bronze badges 2-
d.getFullYear(year, month, day);
It can't work fine. See MDN,getFullYear
doesn't have arguments. – VisioN Commented Feb 18, 2013 at 12:23 -
@VisioN
getFillYear
orgetFullYear
? O.o – Talha Akbar Commented Feb 18, 2013 at 12:24
4 Answers
Reset to default 7NOTE: this answer is assuming a STRING of mm/dd/yyyy
If you use type="date"
in Chrome, you will get strings of 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);
convert date to milliseconds(using valueOf()
) and then substract
DifferNumDays = (date1.valueOf() - date2.valueOf())/(24*60*60*1000);
To get the difference (in days) and pare two dates, you can use these prototype extender methods:
Note: I've distributed the logic into multiple methods that can be used (as helpers) in many different scenarios.
/** 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.pareTo) {
Date.prototype.pareTo = 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
}
}
}
Usage:
//Validate a date object
var dateIsValid = Object.isDate(date1);
//get the difference in days
var numOfDaysBetween = Date.difference(date1, date2);
//pare date to another
var parison = date1.pareTo(date2);
You could convert the dates to milliseconds with getMilliseconds(), subtract them and count it from milliseconds.
本文标签: javascriptHow to compare subtract one date from another and get daysStack Overflow
版权声明:本文标题:javascript - How to compare subtract one date from another and get days? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744860984a2629068.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论