130

The other day at work I stumbled upon this bug and thought it was worth to write a blog post about. Spoiler: It has nothing to do with timezones!

TLDR: According to ISO standard 8601 (which is what Python's date.isocalendar().week uses for example), the first week of the year is the week with the first Thursday of the year. So sometimes the first few days of January belong to the last week of previous year, and sometimes the last few days of December belong to the first week of next year :D

all 43 comments
sorted by: hot top controversial new old
[-] NotMyOldRedditName@lemmy.world 12 points 1 day ago* (last edited 1 day ago)

I hate working with date/time.

[-] NotMyOldRedditName@lemmy.world 5 points 1 day ago

I kid you not, i just had to fix a date time issue around formatting lol. Like minutes after I wrote i hate it lol.

[-] Slashme@lemmy.world 5 points 1 day ago

Similarly, developers have many misconceptions about addresses: https://www.mjt.me.uk/posts/falsehoods-programmers-believe-about-addresses/

[-] zerofk@lemmy.zip 4 points 1 day ago

When we implemented week numbers we actually had to support both systems because customers expected the number to match that in their other applications - some of which followed the standard, and some didn’t.

[-] Valmond@lemmy.dbzer0.com 2 points 1 day ago

So nobody talks about leap seconds? 😅

[-] litchralee@sh.itjust.works 38 points 2 days ago

Obligatory links:

Falsehoods that programmers think about dates and times: https://infiniteundo.com/post/25326999628/falsehoods-programmers-believe-about-time

Tom Scott descends into time and timezone madness: https://www.youtube.com/watch?v=-5wpm-gesOY

I've personally never worked with ISO 8601 directly, and quite frankly prefer how RFC 3339 simply doesn't deal with weeks at all.

[-] null4141@programming.dev 7 points 2 days ago

That video is a classic. It's exactly why I put "it has nothing to do with timezones" as subtitle for the post :) That was my first guess when I encountered the bug, before I realized it was about the ISO weeks.

And in the end of the post I also added a list of falsehoods, different from your link tho: https://gist.github.com/timvisee/fcda9bbdff88d45cc9061606b4b923ca

[-] Skullgrid@lemmy.world 29 points 2 days ago

fuck sake.

thanks for the heads up

[-] folekaule@lemmy.world 16 points 2 days ago

Even better: two weeks with the same week number can exist in the same year.

Fuck, I just wrote an app to help HR process data and I had to, without using Ai, define the first week of the year to match that ISO spec. Week 53 was a fun one but I now have to cover two weeks with the same number.

[-] folekaule@lemmy.world 12 points 2 days ago

Yep, but the year of week will be different for each.

Use a date library. Life is too short to make your own. The new Temporal library in JavaScript is quite good.

[-] bleistift2@sopuli.xyz 3 points 1 day ago

The new Temporal library in JavaScript is quite good.

Sadly, Safari fucks us yet again.

[-] sukhmel@programming.dev 2 points 1 day ago
[-] bleistift2@sopuli.xyz 1 points 20 hours ago

Well, Safari is the only browser not to support the new Temporal API.

[-] sukhmel@programming.dev 2 points 15 hours ago

Ah, I thought they actively broke it somehow

[-] Valmond@lemmy.dbzer0.com 2 points 1 day ago

As all good libraries are 😎

[-] hallettj@leminal.space 5 points 2 days ago

How is that? Do you mean that week 1 can occur twice? Or is there another issue?

IIUC you can have days at the start and the end of the year that are all in week 1, and those are two different weeks. But one of those would be, let's say, week 1 year 2025, and the other would be week 1 year 2026. According to the blog post if you have a week 1 at the end of a year the ISO year number for that week is the next year.

[-] folekaule@lemmy.world 16 points 2 days ago

It only happens with ISO weeks. An example is 2006. Each of the weeks belong to different years (so 2005W52 and 2006W52 are different), but each can contain days from another year. So for example Sunday 2006-01-01 is part of week 52 of 2005. Week 1 starts on January 2. Then at the end of 2006 you have another week 52, but that week is actually part of 2006.

It's a bit of a cheeky thing to point out, because at no point is a day in two different weeks, and the week itself only belongs to one year. It's just that you can't assume that any given day belongs to the same year as the week it is in. That is: 2006-01-01 is in 2005W52 not 2006W01.

[-] null4141@programming.dev 5 points 2 days ago

This is basically the takeaway from my blog post, if you're going to use weeks, use the ISO year instead of the calendar year :)

[-] Pissmidget@lemmy.world 21 points 2 days ago

Week numbers can also vary by country. Not always, but sometimes, as we're not all in agreement on what constitutes the first week of the year.

Something most people in international organisations and teams get to find out the hard way at least once.

[-] bleistift2@sopuli.xyz 17 points 2 days ago

If you think looking at Thursday seems like a random choice: It’s basically a majority vote among the days of the week. If Thursday is in the new year, then 4 days (Thu–Sun) are in the new year. If Thursday is in the old year, then 4 days are in the old year (Mon–Thu).

[-] ttayh@lemmy.zip 17 points 2 days ago

The 1st day of the feek is not universal. I grew up with the week starting on sunday

[-] bleistift2@sopuli.xyz 17 points 2 days ago

Yes, another thing in the neverending list of shit you have to learn at some point if you’re any kind of programmer.

Some people start their week on the wrong day.

[-] TehPers@beehaw.org 3 points 2 days ago

Like ISO 8601. /s

[-] sergiu@lemmy.world 8 points 2 days ago

Is this a thing outside the USA? It always bugs me when apps default to this. How does it make any sense for the week to start with the weekend?

[-] emergencyfood@sh.itjust.works 2 points 22 hours ago

Is this a thing outside the USA?

Yes, most of Asia starts weeks on Sundays.

[-] brisk@aussie.zone 9 points 2 days ago
[-] sergiu@lemmy.world 5 points 2 days ago

A string is reversible, a week is not. A week has a beginning and an end.

[-] technohacker@programming.dev 3 points 2 days ago

I've generally seen Sunday to be the start of the week here, my current headcanon for it is that Sunday and Saturday sort of book-end on both sides of the week, hence week-end_s_ plural

[-] sergiu@lemmy.world 2 points 2 days ago* (last edited 2 days ago)

I'm just curious how this works in practice.

When you say something like "Let's meet up this weekend", is it implied that you will meet on Saturday, since Sunday is the other end of next week, so technically a different weekend?

[-] agamemnonymous@sh.itjust.works 1 points 1 day ago

Honestly I think of Friday night and Saturday. Sunday feels more like its own thing,the beginning of the week. It's the day you do any unfinished chores to prepare for the week. If sometime wanted to "meet up this weekend" I would not assume they meant Sunday, unless they wanted to grab brunch.

[-] locuester@lemmy.zip 1 points 1 day ago

The weekend, and more specifically “this weekend” or “this coming weekend” means both days - Saturday and Sunday.

Sunday being a part of next week on a written calendar does not even register as a potential date “problem” with my brain in day-to-day talk.

[-] technohacker@programming.dev 2 points 2 days ago

Not quite sure actually, I think we end up specifying a day or implicitly consider both of them

[-] bleistift2@sopuli.xyz 1 points 2 days ago

God rested on the seventh day, but Sabbath is on Saturday. Tadaa, week starts on Sunday.

[-] sukhmel@programming.dev 1 points 1 day ago

Yeah, so do we need to make Sunday a work day, too?

[-] spartanatreyu@programming.dev -1 points 2 days ago* (last edited 1 day ago)

Also: January is not always the 1st month, sometimes it is the 0th.

1/1/2026 can be both Jan 1st, and Feb 1st.


For the downvoters, try it in your browser's terminal:

let test = new Date("1-1-2026");

console.log(`Year: ${test.getFullYear()}, Month: ${test.getMonth()}, Day: ${test.getDate()}`);
// Prints -> Year: 2026, Month: 0, Day: 1

//
***
---
***
---
***
---
***
---

// test.getFullYear() returns the year, but test.getYear() only returns the number of years since 1900

// test.getMonth() returns the month, but the first month is 0-indexed

// test.getDate() returns the day, 1-indexed, but test.getDay() returns the current day right now and not the day of the date object

[-] locuester@lemmy.zip 2 points 1 day ago

but the first month is 0-indexed

U mean “the months are zero indexed” and “the first month is 0”

[-] spartanatreyu@programming.dev 0 points 1 day ago* (last edited 1 day ago)

The first month is inputted with a 1 and exported as a 0.

The first day is inputted with a 1 and exported as a 1.

[-] locuester@lemmy.zip 2 points 1 day ago

I understand that. Because the months are zero indexed. But it’s not just the first month that’s “0-indexed”. The list of months is zero indexed. Perhaps I’m arguing semantics - but was simply adding clarity for the reader.

1st becomes 0, 2nd becomes 1, 3rd becomes 2

[-] sukhmel@programming.dev 1 points 1 day ago

Yeah, but when days are 1-indexed and month are 0-indexed you just feel it's a very JavaScript thing to do

[-] locuester@lemmy.zip 2 points 1 day ago

Yeah absolutely. I’m not disagreeing or saying it’s good.

I’m just correcting the strange wording.

this post was submitted on 24 Mar 2026
130 points (97.8% liked)

Programming

26232 readers
534 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



founded 2 years ago
MODERATORS