Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/401.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_Calendar - Fatal编程技术网

用Javascript显示周数?

用Javascript显示周数?,javascript,date,calendar,Javascript,Date,Calendar,我有以下代码,用于显示当天的名称,后跟一个固定短语 <script type="text/javascript"> <!-- // Array of day names var dayNames = new Array( "It's Sunday, the weekend is nearly over", "Yay! Another Monday", "Hello Tuesday, at least you're not M

我有以下代码,用于显示当天的名称,后跟一个固定短语

<script type="text/javascript"> 
    <!-- 
    // Array of day names
    var dayNames = new Array(
    "It's Sunday, the weekend is nearly over",
    "Yay! Another Monday",
     "Hello Tuesday, at least you're not Monday",
     "It's Wednesday. Halfway through the week already",
     "It's Thursday.",
     "It's Friday - Hurray for the weekend",
    "Saturday Night Fever");
    var now = new Date();
    document.write(dayNames[now.getDay()] + ".");
     // -->
</script>

这是从中获取的,但我不知道如何将其添加到现有javascript代码中。

只需将其添加到当前代码中,然后调用
(new Date()).getWeek()


Date.prototype.getWeek=函数(){
var onejan=新日期(this.getFullYear(),0,1);
返回Math.ceil(((this-onejan)/86400000)+onejan.getDay()+1)/7);
}
var weekNumber=(新日期()).getWeek();
var dayNames=[‘星期日’、‘星期一’、‘星期二’、‘星期三’、‘星期四’、‘星期五’、‘星期六’];
var now=新日期();
write(dayNames[now.getDay()]+“(“+weekNumber+”));

使用该代码,您可以

document.write(dayNames[now.getDay()] + " (" + now.getWeek() + ").");

(您需要将
getWeek
函数粘贴到当前脚本上方)

通过添加扩展日期对象的代码段

Date.prototype.getWeek = function() {
    var onejan = new Date(this.getFullYear(),0,1);
    return Math.ceil((((this - onejan) / 86400000) + onejan.getDay()+1)/7);
}

如果您想在多个页面中使用它,您可以将它添加到单独的js文件中,该文件必须在其他脚本执行之前首先加载。对于其他脚本,我指的是使用getWeek()方法的脚本。

考虑使用我的“Date.prototype.getWeek”实现,我认为它比我在这里看到的其他脚本更准确:)

Date.prototype.getWeek=function(){
//我们必须与今年的第一个星期一进行比较,而不是01/01
// 60*60*24*1000 = 86400000
//“onejan\u next\u monday\u time”指01/01后下周一的毫秒
var日_毫秒=86400000,
onejan=新日期(this.getFullYear(),0,1,0,0,0),
onejan\u day=(onejan.getDay()==0)?7:onejan.getDay(),
下周一的天数=(1月8日),
onejan\u next\u monday\u time=onejan.getTime()+(next\u monday的天数*天毫秒),
//如果一个一月不是一个星期一,那就得到一年中的第一个星期一
first\u monday\u year\u time=(onejan\u day>1)?onejan\u next\u monday\u time:onejan.getTime(),
this_date=新日期(this.getFullYear()、this.getMonth()、this.getDate()、0,0,0),//这是00:00:00
this_time=this_date.getTime(),
从第一个星期一算起的天数=数学圆(((本次-第一个星期一年时间)/天(毫秒));
var first_monday_year=新日期(first_monday_year_time);
//我们将“从第一个星期一算起的天数”加上1,因为如果“从第一个星期一算起的天数”是*7,
//然后7/7=1,因为距离第一个星期一还有7天,
//我们应该在第二周而不是第一周(7/7=1)
/我们认为当“星期日第一日”低于0时,周数为52。
//这意味着实际的一周是在第一个星期一之前开始的,所以这意味着我们是在第一周
//一年中的几天(例如:我们在2001年1月1日星期五,然后是“从第一个星期一开始的几天”=-3,
//因此,2001年1月1日星期五是去年第52周的一部分)

//“从第一天到星期一的天数所有建议的方法都可能给出错误的结果,因为它们没有考虑夏季/冬季的时间变化。与其使用86'400'000毫秒的常数计算两个日期之间的天数,不如使用如下方法:

getDaysDiff = function (dateObject0, dateObject1) {
    if (dateObject0 >= dateObject1) return 0;
    var d = new Date(dateObject0.getTime());
    var nd = 0;
    while (d <= dateObject1) {
        d.setDate(d.getDate() + 1);
        nd++;
    }
    return nd-1;
};
getDaysDiff=函数(dateObject0,dateObject1){
如果(dateObject0>=dateObject1)返回0;
var d=新日期(dateObject0.getTime());
var nd=0;

而(d如果您已经使用jquery ui(特别是datepicker):

用法:

var myDate = new Date();
myDate.getWeek();
详情如下:


我意识到这不是一个通用的解决方案,因为它会产生依赖性。然而,考虑到jquery ui的流行性,这可能只是一个简单的适合某人的解决方案-就像对我一样。

看起来我发现的这个函数非常准确且易于使用

// This script is released to the public domain and may be used, modified and
// distributed without restrictions. Attribution not necessary but appreciated.
// Source: http://weeknumber.net/how-to/javascript 

// Returns the ISO week of the date.
Date.prototype.getWeek = function() {
  var date = new Date(this.getTime());
  date.setHours(0, 0, 0, 0);
  // Thursday in current week decides the year.
  date.setDate(date.getDate() + 3 - (date.getDay() + 6) % 7);
  // January 4 is always in week 1.
  var week1 = new Date(date.getFullYear(), 0, 4);
  // Adjust to Thursday in week 1 and count number of weeks from date to week1.
  return 1 + Math.round(((date.getTime() - week1.getTime()) / 86400000 - 3 + (week1.getDay() + 6) % 7) / 7);
}
如果你像我一样幸运,需要找到每月的周数,稍作调整即可:

// Returns the week in the month of the date.
Date.prototype.getWeekOfMonth = function() {
  var date = new Date(this.getTime());
  date.setHours(0, 0, 0, 0);
  // Thursday in current week decides the year.
  date.setDate(date.getDate() + 3 - (date.getDay() + 6) % 7);
  // January 4 is always in week 1.
  var week1 = new Date(date.getFullYear(), date.getMonth(), 4);
  // Adjust to Thursday in week 1 and count number of weeks from date to week1.
  return 1 + Math.round(((date.getTime() - week1.getTime()) / 86400000 - 3 + (week1.getDay() + 6) % 7) / 7);
}

我在这里看到的一些代码在2016年这样的年份失败了,第53周跳到第2周

以下是修订版和工作版:

Date.prototype.getWeek = function() { 

  // Create a copy of this date object  
  var target  = new Date(this.valueOf());  

  // ISO week date weeks start on monday, so correct the day number  
  var dayNr   = (this.getDay() + 6) % 7;  

  // Set the target to the thursday of this week so the  
  // target date is in the right year  
  target.setDate(target.getDate() - dayNr + 3);  

  // ISO 8601 states that week 1 is the week with january 4th in it  
  var jan4    = new Date(target.getFullYear(), 0, 4);  

  // Number of days between target date and january 4th  
  var dayDiff = (target - jan4) / 86400000;    

  if(new Date(target.getFullYear(), 0, 1).getDay() < 5) {
    // Calculate week number: Week 1 (january 4th) plus the    
    // number of weeks between target date and january 4th    
    return 1 + Math.ceil(dayDiff / 7);    
  }
  else {  // jan 4th is on the next week (so next week is week 1)
    return Math.ceil(dayDiff / 7); 
  }
}; 
Date.prototype.getWeek=function(){
//创建此日期对象的副本
var target=新日期(this.valueOf());
//ISO周日期周从周一开始,因此请更正日数
var dayNr=(this.getDay()+6)%7;
//将目标定在本周的星期四,以便
//目标日期在正确的年份
target.setDate(target.getDate()-dayNr+3);
//ISO 8601规定第1周是包含1月4日的一周
var jan4=新日期(target.getFullYear(),0,4);
//目标日期和1月4日之间的天数
var dayDiff=(目标-jan4)/86400000;
if(新日期(target.getFullYear(),0,1).getDay()<5){
//计算周数:第1周(1月4日)加上
//目标日期和1月4日之间的周数
返回1+数学单元(dayDiff/7);
}
否则{//1月4日在下周(因此下周是第1周)
返回Math.ceil(dayDiff/7);
}
}; 

如果您已经使用Angular,那么您可以获利

例如:

var myDate = new Date();
var myWeek = $filter('date')(myDate, 'ww');

如果您想要一些能够工作且经得起未来考验的东西,请使用MomentJS之类的库

moment(date).week();
moment(date).isoWeek()

你会发现这把小提琴很有用。刚刚完成。 代码如下:

/**
*获取ISO周日期周编号
*/  
Date.prototype.getWeek=函数(){
//创建此日期对象的副本
var target=新日期(this.valueOf());
//ISO周日期周从周一开始
//所以请更正日期
var dayNr=(this.getDay()+6)%7;
//ISO 8601规定第1周为一周
//那一年的第一个星期四。
//将目标日期设置为目标周的星期四
target.setDate(target.getDate()-dayNr+3);
//存储目标日期的毫秒值
var firstthready=target.valueOf();
//把目标定在今年的第一个星期四
//首先把目标定在一月一号
目标设定月(0,1);
//不是星期四?对
// Returns the week in the month of the date.
Date.prototype.getWeekOfMonth = function() {
  var date = new Date(this.getTime());
  date.setHours(0, 0, 0, 0);
  // Thursday in current week decides the year.
  date.setDate(date.getDate() + 3 - (date.getDay() + 6) % 7);
  // January 4 is always in week 1.
  var week1 = new Date(date.getFullYear(), date.getMonth(), 4);
  // Adjust to Thursday in week 1 and count number of weeks from date to week1.
  return 1 + Math.round(((date.getTime() - week1.getTime()) / 86400000 - 3 + (week1.getDay() + 6) % 7) / 7);
}
Date.prototype.getWeek = function() { 

  // Create a copy of this date object  
  var target  = new Date(this.valueOf());  

  // ISO week date weeks start on monday, so correct the day number  
  var dayNr   = (this.getDay() + 6) % 7;  

  // Set the target to the thursday of this week so the  
  // target date is in the right year  
  target.setDate(target.getDate() - dayNr + 3);  

  // ISO 8601 states that week 1 is the week with january 4th in it  
  var jan4    = new Date(target.getFullYear(), 0, 4);  

  // Number of days between target date and january 4th  
  var dayDiff = (target - jan4) / 86400000;    

  if(new Date(target.getFullYear(), 0, 1).getDay() < 5) {
    // Calculate week number: Week 1 (january 4th) plus the    
    // number of weeks between target date and january 4th    
    return 1 + Math.ceil(dayDiff / 7);    
  }
  else {  // jan 4th is on the next week (so next week is week 1)
    return Math.ceil(dayDiff / 7); 
  }
}; 
var myDate = new Date();
var myWeek = $filter('date')(myDate, 'ww');
moment(date).week();
moment(date).isoWeek()
ISOWeekday = (0 == InputDate.getDay()) ? 7 : InputDate.getDay();
ISOCalendarWeek = Math.floor( ( ((InputDate.getTime() - (new Date(InputDate.getFullYear(),0,1)).getTime()) / 86400000) - ISOWeekday + 10) / 7 );