javascript 将天数转换为年、月、日
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26159269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert number of days into years, months, days
提问by Rob
I have two date pickers that calculates the number of days there are between the two dates. At the moment I'm outputting the number of days (see code below) which is kind of meaningless. I want to output that number in years, months, days. How can I do that?
我有两个日期选择器,用于计算两个日期之间的天数。目前我正在输出天数(见下面的代码),这是毫无意义的。我想以年、月、日为单位输出该数字。我怎样才能做到这一点?
E.g So 01/01/14 to 01/02/15 = 397 days which then becomes 1 year(s), 1 month(s), 1 day(s)
例如,01/01/14 到 01/02/15 = 397 天,然后变成 1 年、1 个月、1 天
var diff = endDate - startDate;
dayCount = diff / ( 60 * 60 * 24 * 1000 ); // secs * mins * hours * milliseconds
dayCount = Math.round( dayCount ) + this.options.countAdjust;
return dayCount;
回答by Royi Namir
You have a bug in your calculation :
it's 0 month.
And if you mean d/m/y then
1 year, 1 month, and 0 day old.
您的计算中有一个错误:它是 0 个月。如果你的意思是 d/m/y 那么
1 year, 1 month, and 0 day old.
you said between the two dates( not include) - look here
你说 在两个日期之间(不包括) -看这里
Anywayhere is the right code which include actually count each month - how many days it has ! ( leap year consideration):
无论如何,这是正确的代码,其中包括每个月的实际计数 - 它有多少天!(闰年考虑):
notice :
注意 :
I instantiated it as d/m/yyy. feel free to send right pattern in :
我将它实例化为 d/m/yyy。随意发送正确的模式:
alert(getAge( new Date(2014,0,1),new Date(2015,0,2)))
alert(getAge( new Date(2014,0,1),new Date(2015,0,2)))
function getAge(date_1, date_2)
{
//convert to UTC
var date2_UTC = new Date(Date.UTC(date_2.getUTCFullYear(), date_2.getUTCMonth(), date_2.getUTCDate()));
var date1_UTC = new Date(Date.UTC(date_1.getUTCFullYear(), date_1.getUTCMonth(), date_1.getUTCDate()));
var yAppendix, mAppendix, dAppendix;
//--------------------------------------------------------------
var days = date2_UTC.getDate() - date1_UTC.getDate();
if (days < 0)
{
date2_UTC.setMonth(date2_UTC.getMonth() - 1);
days += DaysInMonth(date2_UTC);
}
//--------------------------------------------------------------
var months = date2_UTC.getMonth() - date1_UTC.getMonth();
if (months < 0)
{
date2_UTC.setFullYear(date2_UTC.getFullYear() - 1);
months += 12;
}
//--------------------------------------------------------------
var years = date2_UTC.getFullYear() - date1_UTC.getFullYear();
if (years > 1) yAppendix = " years";
else yAppendix = " year";
if (months > 1) mAppendix = " months";
else mAppendix = " month";
if (days > 1) dAppendix = " days";
else dAppendix = " day";
return years + yAppendix + ", " + months + mAppendix + ", and " + days + dAppendix + " old.";
}
function DaysInMonth(date2_UTC)
{
var monthStart = new Date(date2_UTC.getFullYear(), date2_UTC.getMonth(), 1);
var monthEnd = new Date(date2_UTC.getFullYear(), date2_UTC.getMonth() + 1, 1);
var monthLength = (monthEnd - monthStart) / (1000 * 60 * 60 * 24);
return monthLength;
}
alert(getAge(new Date(2014, 0, 1), new Date(2015, 1, 1)))
回答by Arindam Nayak
You can use link shown below , it has more detailed explanation. JSFIDDLEThe detailed code is -
您可以使用下面显示的链接,它有更详细的解释。JSFIDDLE的详细代码是——
var DateDiff = {
inDays: function(d1, d2) {
var t2 = d2.getTime();
var t1 = d1.getTime();
return parseInt((t2-t1)/(24*3600*1000));
},
inWeeks: function(d1, d2) {
var t2 = d2.getTime();
var t1 = d1.getTime();
return parseInt((t2-t1)/(24*3600*1000*7));
},
inMonths: function(d1, d2) {
var d1Y = d1.getFullYear();
var d2Y = d2.getFullYear();
var d1M = d1.getMonth();
var d2M = d2.getMonth();
return (d2M+12*d2Y)-(d1M+12*d1Y);
},
inYears: function(d1, d2) {
return d2.getFullYear()-d1.getFullYear();
}
}
var d1 = new Date("01/01/14");
var d2 = new Date("01/02/15");
var months= DateDiff.inYears(d1, d2)*12 ;
var month = DateDiff.inMonths(d1, d2) - months;
var days = DateDiff.inYears(d1, d2)*365;
var dy = DateDiff.inDays(d1, d2) - days;
alert(DateDiff.inYears(d1, d2) + " Year " + month + " Month "+ dy + " Days");
回答by Seany84
This has been answered several times befoore: See https://stackoverflow.com/a/17733753/550198
之前已多次回答此问题:参见https://stackoverflow.com/a/17733753/550198
You can modify the following method quite easily to suit your purposes:
您可以很容易地修改以下方法以满足您的目的:
today = new Date()
past = new Date(2010,05,01) // remember this is equivalent to 06 01 2010
//dates in js are counted from 0, so 05 is june
function calcDate(date1,date2) {
var diff = Math.floor(date1.getTime() - date2.getTime());
var day = 1000 * 60 * 60 * 24;
var days = Math.floor(diff/day);
var months = Math.floor(days/31);
var years = Math.floor(months/12);
var message = date2.toDateString();
message += " was "
message += days + " days "
message += months + " months "
message += years + " years ago \n"
return message
}
a = calcDate(today,past)
console.log(a) // returns Tue Jun 01 2010 was 1143 days 36 months 3 years ago
回答by Ajay Kumar Gupta
When you want to get the year and month between two date:
当您想获取两个日期之间的年和月时:
var dateFrom = '2017-08-10';
var dateTo ='2019-04-23';
var date1 = new Date(dateFrom);
var date2 = new Date(dateTo);
var diff=0;
var month=31;
var days=1000*60*60*24;
diff=date2-date1;
var day=(Math.floor(diff/days));
var years = (Math.floor(day/365));
var months = Math.round(day % 365)/month;
document.write(years+"year-"+months);
回答by Fahad Hussain
This will give you the difference between two dates, in milliseconds
这将为您提供两个日期之间的差异,以毫秒为单位
var diff = Math.abs(date1 - date2);
In your example, it'd be
在你的例子中,它会是
var diff = Math.abs(new Date() - compareDate);
You need to make sure that compareDate is a valid Date object.
您需要确保 compareDate 是有效的 Date 对象。
Something like this will probably work for you
像这样的东西可能对你有用
var diff = Math.abs(new Date() - new Date(dateStr.replace(/-/g,'/')));
i.e. turning "2011-02-07 15:13:06" into new Date('2011/02/07 15:13:06'), which is a format the Date constructor can comprehend.
即把“2011-02-07 15:13:06”变成new Date('2011/02/07 15:13:06'),这是Date构造函数可以理解的格式。