Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ';几周前';脚本不占年份_Javascript_Date - Fatal编程技术网

Javascript ';几周前';脚本不占年份

Javascript ';几周前';脚本不占年份,javascript,date,Javascript,Date,标题差不多说明了这一点,我的脚本从一个日期算起有多少周没有计算年份,并且返回了一个坏值 var today = new Date(); var timestamp = new Date($(this).attr('timestamp') * 1000); Date.prototype.getWeeks = function() { var jan = new Date(this.getFullYear(), 0, 1); var now = new Date(this.getF

标题差不多说明了这一点,我的脚本从一个日期算起有多少周没有计算年份,并且返回了一个坏值

var today = new Date();
var timestamp = new Date($(this).attr('timestamp') * 1000);

Date.prototype.getWeeks = function()
{
    var jan = new Date(this.getFullYear(), 0, 1);
    var now = new Date(this.getFullYear(),this.getMonth(),this.getDate());
    var doy = ((now - jan + 1) / 86400000);
    return Math.ceil(doy / 7)
};

console.log('Today: ' + today);                 // Date {Thu Oct 31 2013 09:21:19 GMT-0700 (PDT)}
console.log('PastT: ' + timestamp);             // Date {Fri Nov 20 2009 17:00:00 GMT-0800 (PST)}

console.log('Today: ' + today.getWeeks());      // 44 <-- Should be zero
console.log('PastT: ' + timestamp.getWeeks());  // 47 <-- Not accounting for the Years

console.log('Since: ' + (today.getWeeks() - timestamp.getWeeks())); // time ago = -3
var today=新日期();
var timestamp=新日期($(this).attr('timestamp')*1000);
Date.prototype.getWeeks=函数()
{
var jan=新日期(this.getFullYear(),0,1);
var now=新日期(this.getFullYear()、this.getMonth()、this.getDate());
var doy=((现在-1月+1日)/86400000);
return Math.ceil(doy/7)
};
console.log('Today:'+Today);//日期{2013年10月31日星期四09:21:19 GMT-0700(PDT)}
console.log('PastT:'+时间戳);//日期{2009年11月20日星期五17:00:00 GMT-0800(太平洋标准时间)}

console.log('Today:'+Today.getWeeks());//44您可以比较日期,然后将差值(毫秒)转换为周:

function dateDiffInWeeks(a,b) {
    var _MS_PER_WEEK = 1000 * 60 * 60 * 24 * 7;
    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_WEEK);
}

var today = new Date();
var anotherDay = new Date("Fri Nov 20 2009");

alert(dateDiffInWeeks(today,anotherDay));


披露:这主要是基于。

11月20日是2009年的第47周。是的,我已经找了好几周了,因为现在我在这里的代码中没有看到任何jQuery。这只是纯javascript。@Spudley
$(This).attr('timestamp)
,位于顶部。这里有jQuery。