用 Javascript 显示周数?
我有以下代码,用于显示当天的名称,后跟一组短语。
<script type="text/javascript">
<!--
// Array of day names
var dayNames = new Array(
"It's Sunday, the weekend is nearly over",
"Yay! Another Monday",
"Hello Tuesday, at least you're not Monday",
"It's Wednesday. Halfway through the week already",
"It's Thursday.",
"It's Friday - Hurray for the weekend",
"Saturday Night Fever");
var now = new Date();
document.write(dayNames[now.getDay()] + ".");
// -->
</script>
我想做的是将当前周数放在短语后面的括号中。我找到了以下代码:
Date.prototype.getWeek = function() {
var onejan = new Date(this.getFullYear(),0,1);
return Math.ceil((((this - onejan) / 86400000) + onejan.getDay()+1)/7);
}
取自 http://javascript.about.com/library/blweekyear.htm 但我不知道如何将它添加到现有的 javascript 代码中。
I have the following code that is used to show the name of the current day, followed by a set phrase.
<script type="text/javascript">
<!--
// Array of day names
var dayNames = new Array(
"It's Sunday, the weekend is nearly over",
"Yay! Another Monday",
"Hello Tuesday, at least you're not Monday",
"It's Wednesday. Halfway through the week already",
"It's Thursday.",
"It's Friday - Hurray for the weekend",
"Saturday Night Fever");
var now = new Date();
document.write(dayNames[now.getDay()] + ".");
// -->
</script>
What I would like to do is have the current week number in brackets after the phrase. I have found the following code:
Date.prototype.getWeek = function() {
var onejan = new Date(this.getFullYear(),0,1);
return Math.ceil((((this - onejan) / 86400000) + onejan.getDay()+1)/7);
}
Which was taken from http://javascript.about.com/library/blweekyear.htm but I have no idea how to add it to existing javascript code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
只需将其添加到您当前的代码中,然后调用
(new Date()).getWeek()
Simply add it to your current code, then call
(new Date()).getWeek()
如果您已经使用 jQuery-UI(特别是 datepicker):
用法:
更多信息: UI/Datepicker/iso8601Week
我意识到这不是一个通用的解决方案,因为它会产生依赖性。然而,考虑到 jQuery-UI 的流行,这可能只是适合某些人 - 就像我一样。
如果您不使用 jQuery-UI 并且无意添加依赖项。您只需复制他们的
iso8601Week()
实现,因为它是用纯 JavaScript 编写的,没有复杂的依赖关系:In case you already use jQuery-UI (specifically datepicker):
Usage:
More here: UI/Datepicker/iso8601Week
I realize this isn't a general solution as it incurs a dependency. However, considering the popularity of jQuery-UI this might just be a simple fit for someone - as it was for me.
If you don't use jQuery-UI and have no intention of adding the dependency. You could just copy their
iso8601Week()
implementation since it is written in pure JavaScript without complex dependencies:考虑使用我的“Date.prototype.getWeek”实现,认为比我在这里看到的其他实现更准确:)
您可以在此处查看我的公共存储库https://bitbucket.org/agustinhaller/date.getweek(包括测试)
Consider using my implementation of "Date.prototype.getWeek", think is more accurate than the others i have seen here :)
You can check my public repo here https://bitbucket.org/agustinhaller/date.getweek (Tests included)
如果您想要一些可行且面向未来的东西,请使用像 MomentJS 这样的库。
http://momentjs.com/docs/#/get-set/week/
If you want something that works and is future-proof, use a library like MomentJS.
http://momentjs.com/docs/#/get-set/week/
看起来我在 weeknumber.net 找到的这个函数非常准确且易于使用。
如果您像我一样幸运并且需要找到该月的周数,只需稍加调整即可:
It looks like this function I found at weeknumber.net is pretty accurate and easy to use.
If you're lucky like me and need to find the week number of the month a little adjust will do it:
如果您已经使用 Angular,那么您可以受益
$filter('date')< /代码>
。
例如:
If you already use Angular, then you could profit
$filter('date')
.For example:
我在黑暗中编码(一个挑战),无法查找、引入任何依赖项或测试我的代码。
我忘记了叫什么舍入(Math.celi)所以我想额外确定我是否正确并想出了这个代码。
Just a proof of concept of how you can get the week in any other way
但我仍然推荐 DOM 不需要的任何其他解决方案。
I was coding in the dark (a challenge) and couldn't lookup, bring in any dependencies or test my code.
I forgot what round up was called (Math.celi) So I wanted to be extra sure i got it right and came up with this code instead.
Just a proof of concept of how you can get the week in any other way
But still i recommend any other solution that isn't required by the DOM.
通过添加代码片段,您可以扩展 Date 对象。
如果您想在多个页面中使用它,您可以将其添加到一个单独的 js 文件中,该文件必须在其他脚本执行之前首先加载。对于其他脚本,我指的是使用 getWeek() 方法的脚本。
By adding the snippet you extend the Date object.
If you want to use this in multiple pages you can add this to a seperate js file which must be loaded first before your other scripts executes. With other scripts I mean the scripts which uses the getWeek() method.
所有提出的方法可能会给出错误的结果,因为它们没有考虑夏季/冬季时间的变化。与其使用 86'400'000 毫秒常数来计算两个日期之间的天数,不如使用如下方法:
All the proposed approaches may give wrong results because they don’t take into account summer/winter time changes. Rather than calculating the number of days between two dates using the constant of 86’400’000 milliseconds, it is better to use an approach like the following one:
在支持
week
输入类型的浏览器中,以下函数应该可以工作:Cf.
In browsers that support the
week
input type, the following function should work:Cf.
使用该代码,您可以简单地;
(您需要将
getWeek
函数粘贴到当前脚本上方)With that code you can simply;
(You will need to paste the
getWeek
function above your current script)你会发现这个小提琴很有用。刚刚完成。
https://jsfiddle.net/dnviti/ogpt920w/
下面还有代码:
这是纯JS。
里面有很多日期函数,可以让您将日期转换为周数,反之亦然:)
You could find this fiddle useful. Just finished.
https://jsfiddle.net/dnviti/ogpt920w/
Code below also:
It's pure JS.
There are a bunch of date functions inside that allow you to convert date into week number and viceversa :)
Luxon 是另一种选择。 Luxon 日期对象有一个
weekNumber
属性:Luxon is an other alternative. Luxon date objects have a
weekNumber
property:我已经尝试使用上面所有答案中的代码,并且全部返回一月第一周的第 52 周。所以我决定自己写一个,它可以正确计算周数。
周计数从 0 开始
也许使用循环的品味不好,或者可以将结果缓存在某处,以防止在调用函数足够频繁的情况下重复相同的计算。好吧,我为自己做了这个,它做了我需要它做的事情。
I've tried using code from all of the answers above, and all return week #52 for the first of January. So I decided to write my own, which calculates the week number correctly.
Week numeration starts from 0
Maybe it's a bad taste to use a loop, or the result can be cached somewhere to prevent repeating the same calculations if the function is called often enough. Well, I have made this for myself, and it does what I need it to do.
我在这里看到的一些代码在像 2016 年这样的年份失败,其中第 53 周跳到第 2 周。
这是一个修订后的工作版本:
Some of the code I see in here fails with years like 2016, in which week 53 jumps to week 2.
Here is a revised and working version:
Martin Schillinger 的版本似乎是严格正确的版本。
因为我知道我只需要它在工作日正常工作,所以我根据我在网上找到的东西使用了这个更简单的表格,不记得在哪里:
它在一月初失败,属于上一年最后一周的日子(在这些情况下它会产生 CW = 0)但对于其他所有情况都是正确的。
Martin Schillinger's version seems to be the strictly correct one.
Since I knew I only needed it to work correctly on business week days, I went with this simpler form, based on something I found online, don't remember where:
It fails in early January on days that belong to the previous year's last week (it produces CW = 0 in those cases) but is correct for everything else.