Javascript 如何在JS中获取从日期算起的周数?
我需要找到从某个月到某个日期经过的周数 比如2013年11月(截至2014年1月10日),它应该返回9周 有办法找到它吗?尝试以下方法:Javascript 如何在JS中获取从日期算起的周数?,javascript,jquery,Javascript,Jquery,我需要找到从某个月到某个日期经过的周数 比如2013年11月(截至2014年1月10日),它应该返回9周 有办法找到它吗?尝试以下方法: function differenceInWeeks(d1, d2) { var t2 = d2.getTime(); var t1 = d1.getTime(); return parseInt((t2-t1)/(24*3600*1000*7)); } getTime函数返回自1970/01/01以来的毫秒数,其余的只是数
function differenceInWeeks(d1, d2) {
var t2 = d2.getTime();
var t1 = d1.getTime();
return parseInt((t2-t1)/(24*3600*1000*7));
}
getTime
函数返回自1970/01/01以来的毫秒数,其余的只是数学运算。尝试以下操作:
function differenceInWeeks(d1, d2) {
var t2 = d2.getTime();
var t1 = d1.getTime();
return parseInt((t2-t1)/(24*3600*1000*7));
}
函数返回自1970/01/01以来的毫秒数,其余的只是数学运算。试试这个:
function weeksSince(dateString){
var date = new Date(dateString);
var today = new Date();
return Math.floor((today-date)/(1000*60*60*24*7));
}
console.log(weeksSince("January 01, 2014"));
console.log(weeksSince("January 01, 2013"));
console.log(weeksSince("January 01, 2012"));
=> 1
=> 53
=> 105
试试这个:
function weeksSince(dateString){
var date = new Date(dateString);
var today = new Date();
return Math.floor((today-date)/(1000*60*60*24*7));
}
console.log(weeksSince("January 01, 2014"));
console.log(weeksSince("January 01, 2013"));
console.log(weeksSince("January 01, 2012"));
=> 1
=> 53
=> 105
试试这个:
function weeks_between(date1, date2) {
// The number of milliseconds in one week
var ONE_WEEK = 1000 * 60 * 60 * 24 * 7;
// Convert both dates to milliseconds
var date1_ms = date1.getTime();
var date2_ms = date2.getTime();
// Calculate the difference in milliseconds
var difference_ms = Math.abs(date1_ms - date2_ms);
// Convert back to weeks and return hole weeks
return Math.floor(difference_ms / ONE_WEEK);
}
如果希望它们非常精确(包括天数和时间),请使用以下jquery库:
试试这个:
function weeks_between(date1, date2) {
// The number of milliseconds in one week
var ONE_WEEK = 1000 * 60 * 60 * 24 * 7;
// Convert both dates to milliseconds
var date1_ms = date1.getTime();
var date2_ms = date2.getTime();
// Calculate the difference in milliseconds
var difference_ms = Math.abs(date1_ms - date2_ms);
// Convert back to weeks and return hole weeks
return Math.floor(difference_ms / ONE_WEEK);
}
如果希望它们非常精确(包括天数和时间),请使用以下jquery库:
这有点含糊不清,其他答案都是正确的——只要你知道如何从日期中减去毫秒,这基本上就是数学 先试试这把小提琴
这有点模糊,其他答案都是正确的——只要你知道如何从约会中减去毫秒,这基本上就是数学了 先试试这把小提琴