我正在计算'从'到'到'日期之间的天数。例如,如果起始日期为mm / dd / yyyy且起始日期为mm / dd / yyyy。如何使用JavaScript获得结果?
从日期开始就像:2014年11月25日 到目前为止:2014年11月27日
我需要使用javascript>
将结果作为2答案 0 :(得分:1)
John Resig的实施:
// Get date difference.
// @see http://ejohn.org/projects/javascript-pretty-date/
function prettyDate(date, now){
var diff = (((now || (new Date())).getTime() - date.getTime()) / 1000),
day_diff = Math.floor(diff / 86400);
if (isNaN(day_diff) || day_diff < 0 || day_diff >= 31)
return this.format(date, 'yyyy-mm-dd');
return day_diff == 0 && (
diff < 60 && "Just now" ||
diff < 120 && "1 minute ago" ||
diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
diff < 7200 && "1 hour ago" ||
diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
day_diff == 1 && "yesterday" ||
day_diff < 7 && day_diff + " days ago" ||
day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago";
},
答案 1 :(得分:0)
试试这个:
var date1 = new Date("11/28/2014");
var date2 = new Date("11/27/2014");
var date_difference=parseInt((date1-date2)/(24*3600*1000));
alert(date_difference);
希望它有所帮助!
答案 2 :(得分:0)
解决方案需要的天数比以下: -
var _MS_PER_DAY = 1000 * 60 * 60 * 24;
// a and b are javascript Date objects
function dateDiffInDays(a, b) {
// Discard the time and time-zone information.
var utc1 = Date.UTC(a.getFullYear(), a.getMonth(), a.getDate());
var utc2 = Date.UTC(b.getFullYear(), b.getMonth(), b.getDate());
return Math.floor((utc2 - utc1) / _MS_PER_DAY);
}
答案 3 :(得分:0)
你可以试试这段代码
<script>
var d1 =Date.parse("11/27/2014"); // will give the number of milisenonds passed from 01 January, 1970 00:00:00 Universal Time (UTC)
var d2=Date.parse("11/25/2014");
var miliInDay= 1000*60*60*24; // total milliseconds in 24 hour
var diff= d1-d2;
var dateDiff= diff/miliInDay;
</script>