当您知道星期几时,如何找出该周的开始日期

发布于 2025-01-11 05:13:48 字数 321 浏览 0 评论 0原文

通过“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

匿名。 2025-01-18 05:13:48

您可以使用 I 令牌:

var dateFns = require("date-fns");
console.log(dateFns.parse('10','I', new Date()));

npm.runkit.com 返回周一 7 的日期2022 年 3 月。似乎 date-fns 假定当前星期是年份。我不知道如何指定年份,尝试:

console.log(dateFns.parse('2022-10','YYYY-II', new Date(),{
  useAdditionalWeekYearTokens: true
}));

抛出范围错误:格式字符串不得同时包含 YYYYII 。类似地,对于“2022W10”和标记“YYYY'W'II”,它表示 Y 和 I 标记不能采用相同的格式字符串。

做同样事情的函数是:

// Returns the first day (Monday) of the specified week
// Year defaults to the current local calendar year
function getISOWeek(w, y = new Date().getFullYear()) {
  let d = new Date(y, 0, 4);
  d.setDate(d.getDate() - (d.getDay() || 7) + 1 + 7*(w - 1));
  return d;
}

// Mon Mar 14 2022
console.log(getISOWeek(11).toString());
// Mon Jan 02 2023
console.log(getISOWeek(1,2023).toString());
// Mon Dec 28 2026
console.log(getISOWeek(53,2026).toString());

强大的函数应验证输入,使周数为 1 到 53,并且仅在有 53 周的年份中才允许使用 53。

You can use the I token:

var dateFns = require("date-fns");
console.log(dateFns.parse('10','I', new Date()));

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:

console.log(dateFns.parse('2022-10','YYYY-II', new Date(),{
  useAdditionalWeekYearTokens: true
}));

Throws a range error: The format string mustn't contain YYYY and II 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:

// Returns the first day (Monday) of the specified week
// Year defaults to the current local calendar year
function getISOWeek(w, y = new Date().getFullYear()) {
  let d = new Date(y, 0, 4);
  d.setDate(d.getDate() - (d.getDay() || 7) + 1 + 7*(w - 1));
  return d;
}

// Mon Mar 14 2022
console.log(getISOWeek(11).toString());
// Mon Jan 02 2023
console.log(getISOWeek(1,2023).toString());
// Mon Dec 28 2026
console.log(getISOWeek(53,2026).toString());

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.

那片花海 2025-01-18 05:13:48

我想要一个仅使用 date-fns 函数的解决方案。这就是我组合它的方式:

const result = setWeek(nextMonday(new Date(year, 0, 4)), week, {
    weekStartsOn: 1,
    firstWeekContainsDate: 4,
  });

I wanted a solution only using date-fns functions. This is how i combined it:

const result = setWeek(nextMonday(new Date(year, 0, 4)), week, {
    weekStartsOn: 1,
    firstWeekContainsDate: 4,
  });
撩发小公举 2025-01-18 05:13:48

这是如何为 date-fns 指定年份,它使用提供的日期收集相关信息,例如年份。因此,在解析的相对日期上设置年份可以告诉 date-fns 第 10 周发生在哪一年。

console.log(dateFns.parse('10','I', new Date().setYear(2002)));

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.

console.log(dateFns.parse('10','I', new Date().setYear(2002)));
雪落纷纷 2025-01-18 05:13:48
const weekNumber = 11;
const year = 2022
const weekOfstartDate = dateFns.parse(weekNumber.toString(), "I", new Date().setFullYear(year));  // '2022-03-14'

请注意,结果是 2022-03-14 而不是 2022-03-07,因为 2022 年第 14 周从 14.03 开始

const weekNumber = 11;
const year = 2022
const weekOfstartDate = dateFns.parse(weekNumber.toString(), "I", new Date().setFullYear(year));  // '2022-03-14'

Note that the result is 2022-03-14 but not 2022-03-07 because the 14th week of 2022 starts from 14.03

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文