获取当月的当前周

时间:2017-03-29 23:00:55

标签: javascript php date

我找到了一个javascript和一个php script来获取全年当前的一周。这很棒,但我有点困惑。我想知道,如果本周的1,2,3,4周不是全年。我应该怎么做?

的javascript

/**
 * Returns the week number for this date.  dowOffset is the day of week the week
 * "starts" on for your locale - it can be from 0 to 6. If dowOffset is 1 (Monday),
 * the week returned is the ISO 8601 week number.
 * @param int dowOffset
 * @return int
 */
Date.prototype.getWeek = function (dowOffset) {
/*getWeek() was developed by Nick Baicoianu at MeanFreePath: http://www.meanfreepath.com */

    dowOffset = typeof(dowOffset) == 'int' ? dowOffset : 0; //default dowOffset to zero
    var newYear = new Date(this.getFullYear(),0,1);
    var day = newYear.getDay() - dowOffset; //the day of week the year begins on
    day = (day >= 0 ? day : day + 7);
    var daynum = Math.floor((this.getTime() - newYear.getTime() - 
    (this.getTimezoneOffset()-newYear.getTimezoneOffset())*60000)/86400000) + 1;
    var weeknum;
    //if the year starts before the middle of a week
    if(day < 4) {
        weeknum = Math.floor((daynum+day-1)/7) + 1;
        if(weeknum > 52) {
            nYear = new Date(this.getFullYear() + 1,0,1);
            nday = nYear.getDay() - dowOffset;
            nday = nday >= 0 ? nday : nday + 7;
            /*if the next year starts before the middle of
              the week, it is week #1 of that year*/
            weeknum = nday < 4 ? 1 : 53;
        }
    }
    else {
        weeknum = Math.floor((daynum+day-1)/7);
    }
    return weeknum;
};

用法

var mydate = new Date(2011,2,3); // month number starts from 0
// or like this
var mydate = new Date('March 3, 2011');
alert(mydate.getWeek());

2 个答案:

答案 0 :(得分:1)

ISO周月数基于星期一,因此2017年3月2日星期四是2017年2月第4周,2017年3月6日星期一是3月第1周。

因此,该算法将移至上一个星期一,并查看该月的哪个星期一。我原以为这是重复但我找不到一个,所以这是一个函数。

/* Get ISO week in month, based on first Monday in month
** @param {Date} date - date to get week in month of
** @returns {Object} month: month that week is in
**                   week: week in month
*/
function getISOWeekInMonth(date) {
  // Copy date so don't affect original
  var d = new Date(+date);
  if (isNaN(d)) return;
  // Move to previous Monday
  d.setDate(d.getDate() - d.getDay() + 1);
  // Week number is ceil date/7
  return {month: +d.getMonth()+1,
          week: Math.ceil(d.getDate()/7)};
}
//*
[new Date(2017,2,2),   // Thu 2 Mar 2017
 new Date(2017,2,6),   // Mon 6 Mar 2017
 new Date(2017,4,31),  // Wed 31 May 2017
 new Date()].forEach(  // Current date
  function(date) {
    console.log(date.toString() + '\nis in week ' +
    getISOWeekInMonth(date).week + ' of month ' +
    getISOWeekInMonth(date).month);
  }
);
//*/

如果您想获得一年中的ISO周,那么Get week of year in JavaScript like in PHP就会得到答案。

答案 1 :(得分:0)

这适用于那些只想知道哪一周从1到5的人当月的当周。

<强>脚本

var d = new Date();
var date = d.getDate();
var day = d.getDay();

var weekOfMonth = Math.ceil((date + 6 - day)/7);

document.getElementById('week').value = weekOfMonth;

<强>标记

<input type='text' id='week' value=''>