像在 PHP 中一样,在 JavaScript 中获取一年中的第几周
如何获取一年中的当前周数,如 PHP 的 date('W')
?
它应该是 ISO-8601 一年中的周数,从周开始周一。
How do I get the current weeknumber of the year, like PHP's date('W')
?
It should be the ISO-8601 week number of year, weeks starting on Monday.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(25)
另一个基于库的选项:使用
d3-time-format
< /a>:Another library-based option: use
d3-time-format
:Angular2+ DatePipe 的最短解决方法,针对 ISO-8601 进行了调整:
Shortest workaround for Angular2+ DatePipe, adjusted for ISO-8601:
灵感来自 RobG 的回答。
我想要的是给定日期的星期几。所以我的回答只是基于星期几星期日。但你可以选择其他日子(即周一、周二……);
首先,我找到给定日期中的星期日,然后计算星期。
Inspired from RobG's answer.
What I wanted is the day of the week of a given date. So my answer is simply based on the day of the week Sunday. But you can choose the other day (i.e. Monday, Tuesday...);
First I find the Sunday in a given date and then calculate the week.
根据 https://en.wikipedia.org/wiki/ISO_week_date
date-fns getWeek 执行此操作。
可以根据 wiki 的“元旦前后当代日期示例”表进行测试如下:
在 jsfiddle
results:
According to https://en.wikipedia.org/wiki/ISO_week_date
date-fns getWeek does this.
It can be tested according to wiki's "Examples of contemporary dates around New Year's Day" table as following:
in jsfiddle
results:
您应该能够在这里获得您想要的内容:
http://www.merlyn.demon.co.uk/js-date6.htm#YWD。同一网站上更好的链接是:
工作数周。编辑
这里是一些基于 Dommer 提供的链接和早期发布的代码。它已根据
http://www.merlyn.demon.co.uk/js-date6.htm#YWD上的结果进行了轻微测试。请彻底测试,不提供任何保证。编辑 2017 年
遵守夏令时期间以及 1 月 1 日为星期五的年份存在日期问题。通过使用所有 UTC 方法进行修复。以下代码向 Moment.js 返回相同的结果。
创建“UTC”日期时,小时数将归零。
最小化原型版本(仅返回周数):
测试部分
在此部分中,您可以输入 YYYY-MM-DD 格式的任何日期,并检查此代码是否提供与 Moment.js ISO 周数相同的周数(从 2000 年到 2050 年测试了 50 多年)。
You should be able to get what you want here:
http://www.merlyn.demon.co.uk/js-date6.htm#YWD.A better link on the same site is:
Working with weeks.Edit
Here is some code based on the links provided and that posted eariler by Dommer. It has been lightly tested against results at
http://www.merlyn.demon.co.uk/js-date6.htm#YWD. Please test thoroughly, no guarantee provided.Edit 2017
There was an issue with dates during the period that daylight saving was observed and years where 1 Jan was Friday. Fixed by using all UTC methods. The following returns identical results to Moment.js.
Hours are zeroed when creating the "UTC" date.
Minimized, prototype version (returns only week-number):
Test section
In this section, you can enter any date in YYYY-MM-DD format and check that this code gives the same week number as Moment.js ISO week number (tested over 50 years from 2000 to 2050).
您还可以使用 momentjs 库:
moment().format('W')
You can use momentjs library also:
moment().format('W')
不是 ISO-8601 周数,但如果搜索引擎无论如何都将您指向这里。
如上所述,但没有类:
Not ISO-8601 week number but if the search engine pointed you here anyway.
As said above but without a class:
一致 http://javascript.about.com/library/blweekyear.htm
Accordily http://javascript.about.com/library/blweekyear.htm
下面的代码计算正确的 ISO 8601 周数。它与 PHP 的
date("W")
匹配 1/1/1970 到 1/1/2100 之间的每周。来源: Taco van den Broek
如果您不喜欢扩展原型,那么这里有一个函数:
示例用法:
The code below calculates the correct ISO 8601 week number. It matches PHP's
date("W")
for every week between 1/1/1970 and 1/1/2100.Source: Taco van den Broek
If you're not into extending prototypes, then here's a function:
Sample usage:
Jacob Wright 的
Date.format()
库以 PHPdate()
函数并支持 ISO-8601 周数:可能是对于一周的数字来说有点大材小用,但它确实支持 PHP 样式格式,如果您要做很多这样的事情,它会非常方便。
Jacob Wright's
Date.format()
library implements date formatting in the style of PHP'sdate()
function and supports the ISO-8601 week number:It may be a bit overkill for just a week number, but it does support PHP style formatting and is quite handy if you'll be doing a lot of this.
以下代码与时区无关(使用 UTC 日期),并根据 https://en.wikipedia.org 工作/wiki/ISO_8601
Following code is timezone-independent (UTC dates used) and works according to the https://en.wikipedia.org/wiki/ISO_8601
的周数
获取任何给定日期示例
Get the weeknumber of any given Date
Example
我发现 Oracle 规范中描述的 Java SE 的 SimpleDateFormat 类很有用:
http://goo.gl/7MbCh5。就我而言,在 Google Apps 脚本中,它的工作方式如下:
例如,在电子表格宏中,您可以检索文件的实际时区:
I found useful the Java SE's SimpleDateFormat class described on Oracle's specification:
http://goo.gl/7MbCh5. In my case in Google Apps Script it worked like this:
For example in a spreadsheet macro you can retrieve the actual timezone of the file:
这将“getWeek”方法添加到 Date.prototype,该方法返回从年初开始的周数。该参数定义了首先考虑一周中的哪一天。如果没有通过参数,则假定第一天是星期日。
This adds "getWeek" method to Date.prototype which returns number of week from the beginning of the year. The argument defines which day of the week to consider the first. If no argument passed, first day is assumed Sunday.
此代码修复了 nvd 回答中的错误
this code fixes a bug in answer by nvd
如果您已经在 Angular 项目中,则可以使用
$filter('date')
。例如:
If you are already in an Angular project you could use
$filter('date')
.For example:
这周的数字真是令人痛苦。网络上大多数琐碎的解决方案对我来说并不真正有效,因为它们在大部分时间都有效,但它们都在某个时候崩溃了,特别是当年份发生变化并且一年的最后一周突然成为明年的第一周等时。甚至 Angular 的日期过滤器显示的数据不正确(这是明年的第一周,Angular 给出的是第 53 周)。
注意:这些示例设计用于欧洲周(周一优先)!
getWeek()
setDay()
getDayIndex()
我已经测试过这个,它似乎工作得很好,但如果你注意到它有一个缺陷, 请告诉我。
This week number thing has been a real pain in the a**. Most trivial solutions around the web didn't really work for me as they worked most of the time but all of them broke at some point, especially when year changed and last week of the year was suddenly next year's first week etc. Even Angular's date filter showed incorrect data (it was the 1st week of next year, Angular gave week 53).
Note: The examples are designed to work with European weeks (Mon first)!
getWeek()
setDay()
getDayIndex()
I have tested this and it seems to be working very well but if you notice a flaw in it, please let me know.
对我来说效果很好的代码片段是这样的:
注意:
d
是我想要当前周数的日期。+
将日期转换为数字(使用 TypeScript)。The code snippet which works pretty well for me is this one:
Note:
d
is my Date for which I want the current week number.The
+
converts the Dates into numbers (working with TypeScript).使用 Luxon (https://github.com/moment/luxon) :
With Luxon (https://github.com/moment/luxon) :
这是我在 JavaScript 中计算周数的实现。还纠正了夏季和冬季时间偏移。
我使用了本文中的周定义: ISO 8601
周是从星期一到星期日, 1 月 4 日始终是一年中的第一周。
为了进行测试,我在 Qunit 中使用了以下 JavaScript 测试
Here is my implementation for calculating the week number in JavaScript. corrected for summer and winter time offsets as well.
I used the definition of the week from this article: ISO 8601
Weeks are from mondays to sunday, and january 4th is always in the first week of the year.
for testing i used the following JavaScript tests in Qunit
这是对 Typescript 的轻微修改,它还将返回一周开始和周末的日期。我认为在用户界面中显示这些内容是很常见的,因为人们通常不记得周数。
Here is a slight adaptation for Typescript that will also return the dates for the week start and week end. I think it's common to have to display those in a user interface, since people don't usually remember week numbers.
这是我的打字稿实现,我根据一些日期进行了测试。此实现允许您将一周的第一天设置为任意一天。
测试:
This is my typescript implementation which I tested against some dates. This implementation allows you to set the first day of the week to any day.
Tests:
我做了很多尝试来获得最短的代码来获得符合 ISO 的周数。
变量
date
对于避免改变原始this
是必要的。我使用setDate()
和setMonth()
的返回值来省去getTime()
来节省代码长度,并使用指数数一天的毫秒数,而不是单个元素或带有五个零的数字的乘法。this
是日期或毫秒数,返回值为String
例如“49/2017”。I tried a lot to get the shortest code to get the weeknumber ISO-conform.
The variable
date
is necessary to avoid to alter the originalthis
. I used the return values ofsetDate()
andsetMonth()
to dispense withgetTime()
to save code length and I used an expontial number for milliseconds of a day instead of a multiplication of single elements or a number with five zeros.this
is Date or Number of milliseconds, return value isString
e.g. "49/2017".