可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
This question already has an answer here:
I'm looking for a tested solid solution for getting current week of the year for specified date. All I can find are the ones that doesn't take in account leap years or just plain wrong. Does anyone have this type of stuff?
Or even better a function that says how many weeks does month occupy. It is usually 5, but can be 4 (feb) or 6 (1st is sunday and month has 30-31 days in it)
================= UPDATE:
Still not sure about getting week #, but since I figured out it won't solve my problem with calculating how many weeks month occupy, I abandoned it.
Here's a function to find out how many weeks exactly month occupy on the calendar:
getWeeksNum: function(year, month) { var daysNum = 32 - new Date(year, month, 32).getDate(), fDayO = new Date(year, month, 1).getDay(), fDay = fDayO ? (fDayO - 1) : 6, weeksNum = Math.ceil((daysNum + fDay) / 7); return weeksNum; }
回答1:
/** * 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 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
Usage:
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());
Source
回答2:
Consider using my implementation of "Date.prototype.getWeek", think is more accurate than the others i have seen here :)
Date.prototype.getWeek = function(){ // We have to compare against the first monday of the year not the 01/01 // 60*60*24*1000 = 86400000 // 'onejan_next_monday_time' reffers to the miliseconds of the next monday after 01/01 var day_miliseconds = 86400000, onejan = new Date(this.getFullYear(),0,1,0,0,0), onejan_day = (onejan.getDay()==0) ? 7 : onejan.getDay(), days_for_next_monday = (8-onejan_day), onejan_next_monday_time = onejan.getTime() + (days_for_next_monday * day_miliseconds), // If one jan is not a monday, get the first monday of the year first_monday_year_time = (onejan_day>1) ? onejan_next_monday_time : onejan.getTime(), this_date = new Date(this.getFullYear(), this.getMonth(),this.getDate(),0,0,0),// This at 00:00:00 this_time = this_date.getTime(), days_from_first_monday = Math.round(((this_time - first_monday_year_time) / day_miliseconds)); var first_monday_year = new Date(first_monday_year_time); // We add 1 to "days_from_first_monday" because if "days_from_first_monday" is *7, // then 7/7 = 1, and as we are 7 days from first monday, // we should be in week number 2 instead of week number 1 (7/7=1) // We consider week number as 52 when "days_from_first_monday" is lower than 0, // that means the actual week started before the first monday so that means we are on the firsts // days of the year (ex: we are on Friday 01/01, then "days_from_first_monday"=-3, // so friday 01/01 is part of week number 52 from past year) // "days_from_first_monday= 52 (Math.ceil(366/7)=53) and thats wrong return (days_from_first_monday>=0 && days_from_first_monday
You can check my public repo here https://bitbucket.org/agustinhaller/date.getweek (Tests included)
回答3:
Get week number
Date.prototype.getWeek = function() { var dt = new Date(this.getFullYear(),0,1); return Math.ceil((((this - dt) / 86400000) + dt.getDay()+1)/7); }; var myDate = new Date(2013, 3, 25); // 2013, 25 April console.log(myDate.getWeek());
回答4:
I know this is an old question, but maybe it helps:
http://weeknumber.net/how-to/javascript
回答5:
/*get the week number by following the norms of ISO 8601*/ function getWeek(dt){ var calc=function(o){ if(o.dtmin.getDay()!=1){ if(o.dtmin.getDay()=1 && dt.getDate()=5 || dt.getDay()==0)){ var pyData={"dtmin":new Date(dt.getFullYear()-1,0,1,0,0,0,0),"dtmax":new Date(dt.getFullYear()-1,11,getNbDaysInAMonth(dt.getFullYear()-1,12),0,0,0,0),"w":0}; calc(pyData); return pyData.w; }else{ var ayData={"dtmin":new Date(dt.getFullYear(),0,1,0,0,0,0),"dtmax":new Date(dt.getFullYear(),dt.getMonth(),dt.getDate(),0,0,0,0),"w":0}, nd12m=getNbDaysInAMonth(dt.getFullYear(),12); if(dt.getMonth()==12 && dt.getDay()!=0 && dt.getDay()
回答6:
For those looking for a more simple approach;
Date.prototype.getWeek = function() { var onejan = new Date(this.getFullYear(),0,1); var today = new Date(this.getFullYear(),this.getMonth(),this.getDate()); var dayOfYear = ((today - onejan + 86400000)/86400000); return Math.ceil(dayOfYear/7) };
Use with:
var today = new Date(); var currentWeekNumber = today.getWeek(); console.log(currentWeekNumber);