Javascript 获得本月最佳周数

Javascript 获得本月最佳周数,javascript,jquery,date,Javascript,Jquery,Date,如何使用javascript/jquery获取月份的周数 例如: 第一周:2010年7月5日周数=第一个星期一 上周:2010年7月12日周数=第二个星期一 当前日期:2010年7月19日周数=第三个星期一 下周:2010年7月26日周数=上周一本机不支持此操作 您可以为此推出自己的功能,从每月的第一天开始工作 var currentDate = new Date(); var firstDayOfMonth = new Date( currentDate.getFullYear(), curr

如何使用javascript/jquery获取月份的周数

例如:

第一周:2010年7月5日周数=第一个星期一

上周:2010年7月12日周数=第二个星期一

当前日期:2010年7月19日周数=第三个星期一


下周:2010年7月26日周数=上周一

本机不支持此操作

您可以为此推出自己的功能,从每月的第一天开始工作

var currentDate = new Date();
var firstDayOfMonth = new Date( currentDate.getFullYear(), currentDate.getMonth(), 1 );
然后获取该日期的工作日:

var firstWeekday = firstDayOfMonth.getDay();
。。。这将为您提供一个从0到6的从零开始的索引,其中0是星期日。

功能周和日(日期){
var days=[‘星期日’、‘星期一’、‘星期二’、‘星期三’,
“星期四”、“星期五”、“星期六”],
前缀=[‘第一’、‘第二’、‘第三’、‘第四’、‘第五’];
返回前缀[Math.floor(date.getDate()/7)]+''+天[date.getDay()];
}
console.log(工作日(新日期(2010,7-1,5));//=>“第一个星期一”
console.log(工作日(新日期(2010,7-1,12));//=>“第二个星期一”
console.log(工作日(新日期(2010,7-1,19));//=>“第三个星期一”
console.log(工作日(新日期(2010,7-1,26));//=>“第四个星期一”

log(weekAndDay(newdate())这是一个古老的问题,但现有的答案完全是错误的。这是我的跨浏览器兼容解决方案:

Date.prototype.getWeekOfMonth=函数(精确){
var month=this.getMonth()
,year=this.getFullYear()
,firstWeekday=新日期(年、月、1)。getDay()
,lastDateOfMonth=新日期(年、月+1,0)。getDate()
,offsetDate=this.getDate()+第一个工作日-1
,index=1//从0或1开始索引,由您选择
,weeksInMonth=index+Math.ceil((每月的最后一天+第一个工作日-7)/7)
,周=指数+数学下限(抵销日期/7)
;
if(精确| |周<2+指数)返回周;
返回周===周/月指数+5:周;
};
//将YYYY-MM-DD解析为本地的简单帮助程序
函数parseISOAsLocal(s){
var b=s.split(/\D/);
返回新日期(b[0],b[1]-1,b[2]);
}
//测验
console.log('日期准确|预期不准确|预期');
[   ['2013-02-01', 1, 1],['2013-02-05', 2, 2],['2013-02-14', 3, 3],
['2013-02-23', 4, 4],['2013-02-24', 5, 6],['2013-02-28', 5, 6],
['2013-03-01', 1, 1],['2013-03-02', 1, 1],['2013-03-03', 2, 2],
['2013-03-15', 3, 3],['2013-03-17', 4, 4],['2013-03-23', 4, 4],
['2013-03-24', 5, 5],['2013-03-30', 5, 5],['2013-03-31', 6, 6]
].forEach(功能(测试){
var d=parseISOAsLocal(测试[0])
console.log(测试[0]+''+
d、 getWeekOfMonth(真)+'|'+测试[1]+''+
d、 getWeekOfMonth()+“|”+测试[2];

});我刚刚能够想出一个更简单的代码来计算一年中给定月份的周数

y==年份,例如{2012} m==是来自{0-11}的值

function weeks_Of_Month( y, m ) {
    var first = new Date(y, m,1).getDay();      
    var last = 32 - new Date(y, m, 32).getDate(); 

    // logic to calculate number of weeks for the current month
    return Math.ceil( (first + last)/7 );   
}

我认为这是可行的。它返回一个月中的一周,从0开始:

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

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

这是几年前的事了,但我最近需要使用这个功能,对于2016/2020年的某些日期(如1月31日),这里的代码都不起作用

无论如何,这并不是最有效的,但希望这能帮到别人,因为这是我在这些年里和每隔一年一起工作的唯一方法

Date.prototype.getWeekOfMonth = function () {
    var dayOfMonth = this.getDay();
    var month = this.getMonth();
    var year = this.getFullYear();
    var checkDate = new Date(year, month, this.getDate());
    var checkDateTime = checkDate.getTime();
    var currentWeek = 0;

    for (var i = 1; i < 32; i++) {
        var loopDate = new Date(year, month, i);

        if (loopDate.getDay() == dayOfMonth) {
            currentWeek++;
        }

        if (loopDate.getTime() == checkDateTime) {
            return currentWeek;
        }
    }
};
Date.prototype.getWeekOfMonth=函数(){
var dayOfMonth=this.getDay();
var month=this.getMonth();
var year=this.getFullYear();
var checkDate=新日期(年、月、this.getDate());
var checkDateTime=checkDate.getTime();
var currentWeek=0;
对于(变量i=1;i<32;i++){
var loopDate=新日期(年、月、i);
if(loopDate.getDay()==dayOfMonth){
currentWeek++;
}
if(loopDate.getTime()==checkDateTime){
返回当前周;
}
}
};
功能周编号顺序(日期){
var janOne=新日期(Date.getFullYear(),0,1);
var_date=新日期(date.getFullYear(),date.getMonth(),date.getDate());
var yearDay=(_date-janOne+1)/86400000;//60*60*24*1000
var day=janOne.getUTCDay();

如果(day也在这个话题上挣扎-多亏了Olson.dev!如果有人感兴趣,我将他的函数缩短了一点:

// returns week of the month starting with 0
Date.prototype.getWeekOfMonth = function() {
  var firstWeekday = new Date(this.getFullYear(), this.getMonth(), 1).getDay();
  var offsetDate = this.getDate() + firstWeekday - 1;
  return Math.floor(offsetDate / 7);
}
更新:如果你需要一个本地化版本-你必须调整firstWeekday变量

// german version - week starts with monday
Date.prototype.getWeekOfMonth = function() {
  var firstWeekday = new Date(this.getFullYear(), this.getMonth(), 1).getDay() - 1;
  if (firstWeekday < 0) firstWeekday = 6;
  var offsetDate = this.getDate() + firstWeekday - 1;
  return Math.floor(offsetDate / 7);
}
//德语版本-从星期一开始的一周
Date.prototype.getWeekOfMonth=函数(){
var firstWeekday=新日期(this.getFullYear()、this.getMonth()、1.getDay()-1;
如果(第一个工作日<0)第一个工作日=6;
var offsetDate=this.getDate()+firstWeekday-1;
返回数学楼层(抵销日期/7);
}

我认为您希望使用
weekOfMonth
,因此它将提供每月1-4或1-5周的时间。我解决了这个问题:

var dated = new Date();
var weekOfMonth = (0 | dated.getDate() / 7)+1;

在阅读了所有答案后,我找到了一种比其他方法使用更少的CPU,并且可以在每年每个月的每一天工作的方法。以下是我的代码:

function getWeekInMonth(year, month, day){

    let weekNum = 1; // we start at week 1

    let weekDay = new Date(year, month - 1, 1).getDay(); // we get the weekDay of day 1
    weekDay = weekDay === 0 ? 6 : weekDay-1; // we recalculate the weekDay (Mon:0, Tue:1, Wed:2, Thu:3, Fri:4, Sat:5, Sun:6)

    let monday = 1+(7-weekDay); // we get the first monday of the month

    while(monday <= day) { //we calculate in wich week is our day
        weekNum++;
        monday += 7;
    }

    return weekNum; //we return it
}
函数getWeekInMonth(年、月、日){
让weekNum=1;//我们从第1周开始
让weekDay=新日期(年,月-1,1).getDay();//我们得到第一天的工作日
weekDay=weekDay===0?6:weekDay-1;//我们重新计算工作日(周一:0,周二:1,周三:2,周四:3,周五:4,周六:5,周日:6)
让星期一=1+(7个工作日);//我们得到一个月的第一个星期一

而(星期一请尝试下面的函数。此函数将周开始日期视为星期一,周结束日期视为星期日

getWeekNumber(date) {
    var monthStartDate =new Date(new Date().getFullYear(), new 
         Date().getMonth(), 1);
    monthStartDate = new Date(monthStartDate);
    var day = startdate.getDay();
    date = new Date(date);
    var date = date.getDate();
    return Math.ceil((date+ day-1)/ 7);
}
我希望这能奏效 测试至2025年

import getWeekOfMonth from 'date-fns/getWeekOfMonth'
...
let weekOfMonth = getWeekOfMonth(new Date())

以前的解决方案都没有在响应中包含“最后一个”,我可以通过检查一周中同一天的下一个事件和c来完成
function getWeekInMonth(year, month, day){

    let weekNum = 1; // we start at week 1

    let weekDay = new Date(year, month - 1, 1).getDay(); // we get the weekDay of day 1
    weekDay = weekDay === 0 ? 6 : weekDay-1; // we recalculate the weekDay (Mon:0, Tue:1, Wed:2, Thu:3, Fri:4, Sat:5, Sun:6)

    let monday = 1+(7-weekDay); // we get the first monday of the month

    while(monday <= day) { //we calculate in wich week is our day
        weekNum++;
        monday += 7;
    }

    return weekNum; //we return it
}
getWeekNumber(date) {
    var monthStartDate =new Date(new Date().getFullYear(), new 
         Date().getMonth(), 1);
    monthStartDate = new Date(monthStartDate);
    var day = startdate.getDay();
    date = new Date(date);
    var date = date.getDate();
    return Math.ceil((date+ day-1)/ 7);
}
function getWeekOfMonth(date) {
  const startWeekDayIndex = 1; // 1 MonthDay 0 Sundays
  const firstDate = new Date(date.getFullYear(), date.getMonth(), 1);
  const firstDay = firstDate.getDay();

  let weekNumber = Math.ceil((date.getDate() + firstDay) / 7);
  if (startWeekDayIndex === 1) {
    if (date.getDay() === 0 && date.getDate() > 1) {
      weekNumber -= 1;
    }

    if (firstDate.getDate() === 1 && firstDay === 0 && date.getDate() > 1) {
      weekNumber += 1;
    }
  }
  return weekNumber;
}
import getWeekOfMonth from 'date-fns/getWeekOfMonth'
...
let weekOfMonth = getWeekOfMonth(new Date())