我如何获得一年中的当前周数,比如 PHP 的date('W')
?
它应该是ISO-8601 年中的周数,从星期一开始。
我如何获得一年中的当前周数,比如 PHP 的date('W')
?
它应该是ISO-8601 年中的周数,从星期一开始。
你应该能够在这里得到你想要的东西:http : //www.merlyn.demon.co.uk/js-date6.htm#YWD。
同一站点上更好的链接是:Working with week。
这是一些基于提供的链接和 Dommer 早期发布的代码。它已针对http://www.merlyn.demon.co.uk/js-date6.htm#YWD 上的结果进行了轻微测试。请彻底测试,不提供任何保证。
在实行夏令时期间的日期和 1 月 1 日是星期五的年份存在问题。通过使用所有 UTC 方法修复。下面将向 Moment.js 返回相同的结果。
/* For a given date, get the ISO week number
*
* Based on information at:
*
* http://www.merlyn.demon.co.uk/weekcalc.htm#WNR
*
* Algorithm is to find nearest thursday, it's year
* is the year of the week number. Then get weeks
* between that date and the first day of that year.
*
* Note that dates in one year can be weeks of previous
* or next year, overlap is up to 3 days.
*
* e.g. 2014/12/29 is Monday in week 1 of 2015
* 2012/1/1 is Sunday in week 52 of 2011
*/
function getWeekNumber(d) {
// Copy date so don't modify original
d = new Date(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate()));
// Set to nearest Thursday: current date + 4 - current day number
// Make Sunday's day number 7
d.setUTCDate(d.getUTCDate() + 4 - (d.getUTCDay()||7));
// Get first day of year
var yearStart = new Date(Date.UTC(d.getUTCFullYear(),0,1));
// Calculate full weeks to nearest Thursday
var weekNo = Math.ceil(( ( (d - yearStart) / 86400000) + 1)/7);
// Return array of year and week number
return [d.getUTCFullYear(), weekNo];
}
var result = getWeekNumber(new Date());
document.write('It\'s currently week ' + result[1] + ' of ' + result[0]);
创建“UTC”日期时,小时数归零。
最小化的原型版本(仅返回周数):
Date.prototype.getWeekNumber = function(){
var d = new Date(Date.UTC(this.getFullYear(), this.getMonth(), this.getDate()));
var dayNum = d.getUTCDay() || 7;
d.setUTCDate(d.getUTCDate() + 4 - dayNum);
var yearStart = new Date(Date.UTC(d.getUTCFullYear(),0,1));
return Math.ceil((((d - yearStart) / 86400000) + 1)/7)
};
document.write('The current ISO week number is ' + new Date().getWeekNumber());
在本节中,您可以输入 YYYY-MM-DD 格式的任何日期,并检查此代码是否提供与 Moment.js ISO 周数相同的周数(从 2000 年到 2050 年测试了 50 多年)。
Date.prototype.getWeekNumber = function(){
var d = new Date(Date.UTC(this.getFullYear(), this.getMonth(), this.getDate()));
var dayNum = d.getUTCDay() || 7;
d.setUTCDate(d.getUTCDate() + 4 - dayNum);
var yearStart = new Date(Date.UTC(d.getUTCFullYear(),0,1));
return Math.ceil((((d - yearStart) / 86400000) + 1)/7)
};
function checkWeek() {
var s = document.getElementById('dString').value;
var m = moment(s, 'YYYY-MM-DD');
document.getElementById('momentWeek').value = m.format('W');
document.getElementById('answerWeek').value = m.toDate().getWeekNumber();
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
Enter date YYYY-MM-DD: <input id="dString" value="2021-02-22">
<button onclick="checkWeek(this)">Check week number</button><br>
Moment: <input id="momentWeek" readonly><br>
Answer: <input id="answerWeek" readonly>
您也可以使用 momentjs 库:
不是 ISO-8601 周数,但如果搜索引擎将您指向这里。
如上所述,但没有类:
let now = new Date();
let onejan = new Date(now.getFullYear(), 0, 1);
let week = Math.ceil((((now.getTime() - onejan.getTime()) / 86400000) + onejan.getDay() + 1) / 7);
console.log(week);
相应地http://javascript.about.com/library/blweekyear.htm
Date.prototype.getWeek = function() {
var onejan = new Date(this.getFullYear(),0,1);
var millisecsInDay = 86400000;
return Math.ceil((((this - onejan) /millisecsInDay) + onejan.getDay()+1)/7);
};
Jacob Wright 的Date.format()
库实现了 PHPdate()
函数风格的日期格式,并支持 ISO-8601 周数:
new Date().format('W');
对于仅一周的数字来说,这可能有点矫枉过正,但它确实支持 PHP 样式格式,如果您要进行大量此类操作,它会非常方便。