当您知道星期几时,如何找出该周的开始日期
通过“date-fns”模块,我收到了今年日期有多少周的数字。
const current = '2022-03-10'
const weekNumber = getWeek(current, 1) // 11
相反,如果你只知道数字,你想知道如何做星期数字的第一个日期。
我想知道的方式。
const weekNumber = 11;
const weekOfstartDate = anyFunc(weekNumber) // '2022-03-07'
你知道这个解决方案的答案吗?
Through the 'date-fns' module, I am receiving numbers of how many weeks the date is this year.
const current = '2022-03-10'
const weekNumber = getWeek(current, 1) // 11
On the contrary, if you only know the numbers, you want to know how to do the first date of the week number.
The way I want to know.
const weekNumber = 11;
const weekOfstartDate = anyFunc(weekNumber) // '2022-03-07'
Do you know the answer to this solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 I 令牌:
在 npm.runkit.com 返回周一 7 的日期2022 年 3 月。似乎 date-fns 假定当前星期是年份。我不知道如何指定年份,尝试:
抛出范围错误:格式字符串不得同时包含
YYYY
和II
。类似地,对于“2022W10”和标记“YYYY'W'II”,它表示 Y 和 I 标记不能采用相同的格式字符串。做同样事情的函数是:
强大的函数应验证输入,使周数为 1 到 53,并且仅在有 53 周的年份中才允许使用 53。
You can use the I token:
At npm.runkit.com that returns a date for Mon 7 Mar 2022. It seems date-fns assumes the year of the current week. I have no idea how to specify the year, attempting:
Throws a range error: The format string mustn't contain
YYYY
andII
at the same time. Similarly for "2022W10" and tokens "YYYY'W'II", it says Y and I tokens can't be in the same format string.A function to do the same thing is:
A robust function should validate the input such that the week number is 1 to 53 and that 53 is only permitted for years that have 53 weeks.
我想要一个仅使用 date-fns 函数的解决方案。这就是我组合它的方式:
I wanted a solution only using date-fns functions. This is how i combined it:
这是如何为 date-fns 指定年份,它使用提供的日期收集相关信息,例如年份。因此,在解析的相对日期上设置年份可以告诉 date-fns 第 10 周发生在哪一年。
This is how to specify year for date-fns it uses date provided gather relevant information such as year. So setting year on relative date for parse tells date-fns which year that 10th week is occuring in.
请注意,结果是
2022-03-14
而不是2022-03-07
,因为 2022 年第 14 周从 14.03 开始Note that the result is
2022-03-14
but not2022-03-07
because the 14th week of 2022 starts from 14.03