Pra*_*sad 20 javascript jquery date
如何使用javascript/jquery获取周数?
例如:
第一周:2010年7月5日./周数= 第一个星期一
上周:2010年7月12日./周数= 第二个星期一
当前日期:2010年7月19日./周数= 第三个星期一
下周:2010年7月26日./周数= 上周一
Ols*_*dev 31
这是一个古老的问题,但存在的答案是完全错误的.这是我的跨浏览器兼容解决方案:
Date.prototype.getWeekOfMonth = function(exact) {
var month = this.getMonth()
, year = this.getFullYear()
, firstWeekday = new Date(year, month, 1).getDay()
, lastDateOfMonth = new Date(year, month + 1, 0).getDate()
, offsetDate = this.getDate() + firstWeekday - 1
, index = 1 // start index at 0 or 1, your choice
, weeksInMonth = index + Math.ceil((lastDateOfMonth + firstWeekday - 7) / 7)
, week = index + Math.floor(offsetDate / 7)
;
if (exact || week < 2 + index) return week;
return week === weeksInMonth ? index + 5 : week;
};
// Simple helper to parse YYYY-MM-DD as local
function parseISOAsLocal(s){
var b = s.split(/\D/);
return new Date(b[0],b[1]-1,b[2]);
}
// Tests
console.log('Date Exact|expected not exact|expected');
[ ['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(function(test){
var d = parseISOAsLocal(test[0])
console.log(test[0] + ' ' +
d.getWeekOfMonth(true) + '|' + test[1] + ' ' +
d.getWeekOfMonth() + '|' + test[2]);
});Run Code Online (Sandbox Code Playgroud)
如果您不想,则无需将其直接放在原型上.在我的实现中,6表示"最后",而不是"第六".如果您希望它始终返回该月的实际周,那么只需通过即可true.
编辑:修复此问题以处理5周和6周的月份.我的"单元测试",随意分叉:http://jsfiddle.net/OlsonDev/5mXF6/1/.
我认为这有效。它返回一月中的第几周,从 0 开始:
var d = new Date();
var date = d.getDate();
var day = d.getDay();
var weekOfMonth = Math.ceil((date - 1 - day) / 7);
Run Code Online (Sandbox Code Playgroud)
也在这个主题上挣扎-感谢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);
}
Run Code Online (Sandbox Code Playgroud)
更新:如果您需要本地化版本-您必须调整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);
}
Run Code Online (Sandbox Code Playgroud)
Jam*_*mes -1
function weekAndDay(date) {
var days = ['Sunday','Monday','Tuesday','Wednesday',
'Thursday','Friday','Saturday'],
prefixes = ['First', 'Second', 'Third', 'Fourth', 'Fifth'];
return prefixes[Math.floor(date.getDate() / 7)] + ' ' + days[date.getDay()];
}
console.log( weekAndDay(new Date(2010,7-1, 5)) ); // => "First Monday"
console.log( weekAndDay(new Date(2010,7-1,12)) ); // => "Second Monday"
console.log( weekAndDay(new Date(2010,7-1,19)) ); // => "Third Monday"
console.log( weekAndDay(new Date(2010,7-1,26)) ); // => "Fourth Monday"
console.log( weekAndDay(new Date()) );Run Code Online (Sandbox Code Playgroud)
添加该功能Last ...可能需要更多的黑客攻击......
| 归档时间: |
|
| 查看次数: |
46761 次 |
| 最近记录: |