获取当前月份的当前星期 [英] Getting current week of current month

查看:167
本文介绍了获取当前月份的当前星期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了 javascript 和php 脚本,以获取整年的当前月份。很好,但我有点困惑。我想知道是否是本月的第1,2,3,4周,而不是全年。我应该怎么知道?

I found a javascript and a php script to get current week of the month base on the entire year. That's great but im a little confused. I would like to know whether if it's week 1,2,3,4 of current month not entire year. How should I got about that?

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());


推荐答案

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

The ISO week in month number is based on Mondays, so Thursday, 2 March 2017 is in week 4 of February 2017 and Monday 6 March 2017 is in week 1 of March.

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

So the algorithm is to move to the previous Monday and see which Monday of the month it is. I would have thought this was a duplicate but I can't find one, so here's a function.

/* 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周一年,然后 使用JavaScript来获取一年中的某周在PHP 中有您的答案。

If you want to get the ISO week in the year, then Get week of year in JavaScript like in PHP has your answer.

这篇关于获取当前月份的当前星期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆