最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Discrepancy between date-fns startOfWeek and format W - Stack Overflow

programmeradmin1浏览0评论

I'm having a problem with date-fns and the way it handles "start of weeks".

The "start of week" of 2015-01-01, according to my calendar, was 2014-12-29.

But, date-fns#startOfWeek tells me otherwise:

❯❯❯ dateFns.startOfWeek(new Date('2015-01-01'))
Sun Dec 28 2014 00:00:00 GMT+0100 (Central European Standard Time)

If I format the result of startOfWeek (2014-12-28) to just return the week number, what I get is "52" (which doesn't match with the result provided by dateFns#startOfWeek):

❯❯❯ dateFns.format(new Date('2014-12-28'), 'W')
"52"

But, if I format the original date (2015-01-01), it will return "1":

❯❯❯ dateFns.format(new Date('2015-01-01'), 'W')
"1"

Why is there this discrepancy? What's the proper way to get the right "start of week" date and week number?

Note, I'm setting TZ=Etc/UTC in my environment variables when I run the above commands on my Node.js CLI. So it shouldn't be a timezone issue.

I'm having a problem with date-fns and the way it handles "start of weeks".

The "start of week" of 2015-01-01, according to my calendar, was 2014-12-29.

But, date-fns#startOfWeek tells me otherwise:

❯❯❯ dateFns.startOfWeek(new Date('2015-01-01'))
Sun Dec 28 2014 00:00:00 GMT+0100 (Central European Standard Time)

If I format the result of startOfWeek (2014-12-28) to just return the week number, what I get is "52" (which doesn't match with the result provided by dateFns#startOfWeek):

❯❯❯ dateFns.format(new Date('2014-12-28'), 'W')
"52"

But, if I format the original date (2015-01-01), it will return "1":

❯❯❯ dateFns.format(new Date('2015-01-01'), 'W')
"1"

Why is there this discrepancy? What's the proper way to get the right "start of week" date and week number?

Note, I'm setting TZ=Etc/UTC in my environment variables when I run the above commands on my Node.js CLI. So it shouldn't be a timezone issue.

Share Improve this question edited Mar 19, 2019 at 13:01 Fez Vrasta asked Mar 19, 2019 at 12:46 Fez VrastaFez Vrasta 14.8k24 gold badges108 silver badges175 bronze badges 3
  • I personally find moment.js more developer friendly – carkod Commented Mar 19, 2019 at 12:59
  • 1 @carkod I'm migrating from Moment.js to date-fns, I need to reduce the size of the bundle. – Fez Vrasta Commented Mar 19, 2019 at 13:00
  • The first day of the week is an administrative thing. In some places it's Sunday, others it's Monday, in some it's Saturday. There may be others. – RobG Commented Mar 20, 2019 at 14:16
Add a comment  | 

2 Answers 2

Reset to default 20

What you are getting is correct. By default, a week starts on Sunday. But if you want to start it from Monday, you can do it like this:

var result = startOfWeek(new Date(2014, 8, 2, 11, 55, 0), {weekStartsOn: 1})

Reference

Example:

var result = dateFns.startOfWeek(new Date('2015-01-01'))
console.log(result);

result = dateFns.startOfWeek(new Date('2015-01-01'), {weekStartsOn: 1})
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/date-fns/1.30.1/date_fns.min.js"></script>

Concerning the week numbers, W in format() gives you ISO week number, meaning it starts form Monday. Moreover there were only 52 weeks in 2014.

Source: https://www.epochconverter.com/weeks/2014

I don't know when it was added but you can also use startOfISOWeek instead of startOfWeek for date-fns in 2021.

startOfISOWeek returns the date of Monday instead of Sunday as start of the week.

发布评论

评论列表(0)

  1. 暂无评论