需要更正或建议 javascript 来计算两个日期之间的差异
Need correction or suggestion over javascript that calculate difference in between two dates
年龄计算
我从许多网站上收集了这个JavaScript;我想获得两个不同日期之间的年龄;
在这个脚本或示例中,我希望向我的女儿展示 age;;一切顺利 直到她的年龄超过 6 个月,
该脚本将年龄四舍五入到上面的数字,即
(在这种情况下为一年) 和
向下舍入显示月份数字的月份。
好吧,如果 少于 365 天 ,我希望脚本查看四舍五入到较小数字的年份,
以及自少于 365 天.
后的月份 四舍五入
<!-- Calculate Age -->
<script type="text/javascript">
var s = '2016-01-31T17:17:28.593Z';
var a = s.split(/[^0-9]/);
var d=new Date (a[0],a[1]-1,a[2],a[3],a[4],a[5] );
var fromDate = new Date(d);
var toDate = new Date();
// current date format as mm-dd-yyyy
var Start_Date = new Date(d);
var sec = Start_Date.getSeconds();
var min = Start_Date.getMinutes();
var hr = Start_Date.getHours();
var dd = Start_Date.getDate();
var mm = Start_Date.getMonth()+1; //January is 0!
var yyyy = Start_Date.getFullYear();
if(dd<10){
dd='0'+dd
}
if(mm<10){
mm='0'+mm
}
if(hr<10){
hr='0'+hr
}
if(min<10){
min='0'+min
}
var Start_Date = dd+'-'+mm+'-'+yyyy+'  at <span style="direction:ltr; color:red ;font-weight:700;">[ '+hr+ ":" +min+ ":" +sec+' ]</span>';
// End date format as mm-dd-yyyy
// Start_Date = str;
var differenceTravel = toDate.getTime() - fromDate.getTime();
// var months = Math.ceil (toDate.getMonth() - fromDate.getMonth() + (12 * (toDate.getFullYear() -
// 1 day = 86400000,
var Tyears = Math.round(differenceTravel/1000/60/60/24/365);
var Tmonths = Math.round((differenceTravel - (Tyears * 365*60*60*24*1000)) / (1000*60*60*24*30));
var Tdays = Math.round((differenceTravel - (Tyears *60*60*24*365*1000) - (Tmonths *60*60*24*30*1000))/ (86400000));
//
document.writeln('<div style="color:Blue">');
document.writeln('<ul>');
document.writeln('<li><span style="color:Navy">Seba Birthday: </span> ' + Start_Date +'</li>');
document.write('<li><span style="color:MediumVioletRed">Her age is:</span> '+ Tyears +'<span style="color:Green"> year and </span>'+ Tmonths +'<span style="color:Green"> months and </span>'+ Tdays +'<span style="color:Green"> days</span></li>');
document.writeln('</ul></div>');
</script>
<!-- / good -->
如果要向下舍入,请使用 Math.floor,而不是 Math.round
,这将根据值是 <
还是 >
而向上或向下舍入0.5
.
更改这三行:
var Tyears = Math.round(differenceTravel/1000/60/60/24/365);
var Tmonths = Math.round((differenceTravel - (Tyears * 365*60*60*24*1000)) / (1000*60*60*24*30));
var Tdays = Math.round((differenceTravel - (Tyears *60*60*24*365*1000) - (Tmonths *60*60*24*30*1000))/ (86400000));
以下内容:
var Tyears = Math.floor(differenceTravel/1000/60/60/24/365);
var Tmonths = Math.ceil((differenceTravel - (Tyears * 365*60*60*24*1000)) / (1000*60*60*24*30));
var Tdays = Math.round((differenceTravel - (Tyears *60*60*24*365*1000) - (Tmonths *60*60*24*30*1000))/ (86400000));
年龄计算
我从许多网站上收集了这个JavaScript;我想获得两个不同日期之间的年龄;
在这个脚本或示例中,我希望向我的女儿展示 age;;一切顺利 直到她的年龄超过 6 个月,
该脚本将年龄四舍五入到上面的数字,即
(在这种情况下为一年) 和
向下舍入显示月份数字的月份。
好吧,如果 少于 365 天 ,我希望脚本查看四舍五入到较小数字的年份,
以及自少于 365 天.
<!-- Calculate Age -->
<script type="text/javascript">
var s = '2016-01-31T17:17:28.593Z';
var a = s.split(/[^0-9]/);
var d=new Date (a[0],a[1]-1,a[2],a[3],a[4],a[5] );
var fromDate = new Date(d);
var toDate = new Date();
// current date format as mm-dd-yyyy
var Start_Date = new Date(d);
var sec = Start_Date.getSeconds();
var min = Start_Date.getMinutes();
var hr = Start_Date.getHours();
var dd = Start_Date.getDate();
var mm = Start_Date.getMonth()+1; //January is 0!
var yyyy = Start_Date.getFullYear();
if(dd<10){
dd='0'+dd
}
if(mm<10){
mm='0'+mm
}
if(hr<10){
hr='0'+hr
}
if(min<10){
min='0'+min
}
var Start_Date = dd+'-'+mm+'-'+yyyy+'  at <span style="direction:ltr; color:red ;font-weight:700;">[ '+hr+ ":" +min+ ":" +sec+' ]</span>';
// End date format as mm-dd-yyyy
// Start_Date = str;
var differenceTravel = toDate.getTime() - fromDate.getTime();
// var months = Math.ceil (toDate.getMonth() - fromDate.getMonth() + (12 * (toDate.getFullYear() -
// 1 day = 86400000,
var Tyears = Math.round(differenceTravel/1000/60/60/24/365);
var Tmonths = Math.round((differenceTravel - (Tyears * 365*60*60*24*1000)) / (1000*60*60*24*30));
var Tdays = Math.round((differenceTravel - (Tyears *60*60*24*365*1000) - (Tmonths *60*60*24*30*1000))/ (86400000));
//
document.writeln('<div style="color:Blue">');
document.writeln('<ul>');
document.writeln('<li><span style="color:Navy">Seba Birthday: </span> ' + Start_Date +'</li>');
document.write('<li><span style="color:MediumVioletRed">Her age is:</span> '+ Tyears +'<span style="color:Green"> year and </span>'+ Tmonths +'<span style="color:Green"> months and </span>'+ Tdays +'<span style="color:Green"> days</span></li>');
document.writeln('</ul></div>');
</script>
<!-- / good -->
如果要向下舍入,请使用 Math.floor,而不是 Math.round
,这将根据值是 <
还是 >
而向上或向下舍入0.5
.
更改这三行:
var Tyears = Math.round(differenceTravel/1000/60/60/24/365);
var Tmonths = Math.round((differenceTravel - (Tyears * 365*60*60*24*1000)) / (1000*60*60*24*30));
var Tdays = Math.round((differenceTravel - (Tyears *60*60*24*365*1000) - (Tmonths *60*60*24*30*1000))/ (86400000));
以下内容:
var Tyears = Math.floor(differenceTravel/1000/60/60/24/365);
var Tmonths = Math.ceil((differenceTravel - (Tyears * 365*60*60*24*1000)) / (1000*60*60*24*30));
var Tdays = Math.round((differenceTravel - (Tyears *60*60*24*365*1000) - (Tmonths *60*60*24*30*1000))/ (86400000));