DEV Community

Aleksandar Sabo
Aleksandar Sabo

Posted on • Originally published at softwarewitchcraft.com on

Determining the Date of Monday in the First Week of the Year

I had to create a script that generates all weeks in a year and writes their start and end dates. While searching for a solution, I discovered an interesting algorithm that determines the date of Monday in the first week of the year. This seemingly simple task becomes more complex when considering international standards and varying definitions of the “first week.”

Problem Definition

The problem is straightforward:

  • Input: A year (e.g., 2025).

  • Output: The date of Monday in the first week of that year.

This isn’t as simple as selecting January 1st because the first week of the year isn’t always the one containing January 1st. This brings us to the ISO 8601 standard.

First week in the ISO 8601 Standard

ISO 8601 defines the first week of the year as the week that contains the first Thursday of the year. This is equivalent to the week that contains January 4th. According to this standard:

  • A week starts on Monday.

  • The first week of the year is the one that includes January 4th.

This means:

  • If January 1st falls on a Monday through Thursday, it’s in the first week of the year.

  • If January 1st falls on a Friday, Saturday, or Sunday, the first Monday of the first ISO week will be in the following calendar week.

Simple Solution in Any Programming Language

To solve this problem, follow these simple steps:

  1. Identify January 4th of the given year. This date is guaranteed to be in the first ISO week.

  2. Determine the weekday of January 4th. (0 for Monday, 1 for Tuesday, …, 6 for Sunday, depending on the language.)

  3. Subtract the necessary number of days to get back to Monday.

Pseudocode Implementation

function getFirstMondayOfYear(year):
    jan4 = Date(year, 1, 4)
    dayOfWeek = jan4.weekday()  // Assuming 0 = Monday, 6 = Sunday
    firstMonday = jan4 - (dayOfWeek) days
    return firstMonday
Enter fullscreen mode Exit fullscreen mode

PHP Implementation

function getFirstMondayOfYear($year) {
    $jan4 = new DateTime("$year-01-04");
    $dayOfWeek = (int)$jan4->format('N') - 1; // N: 1 (Monday) to 7 (Sunday), adjust to 0-6
    $jan4->modify("-{$dayOfWeek} days");
    return $jan4->format('Y-m-d');
}

echo getFirstMondayOfYear(2025);
Enter fullscreen mode Exit fullscreen mode

JavaScript Implementation

function getFirstMondayOfYear(year) {
    const jan4 = new Date(year, 0, 4); // January is month 0
    const dayOfWeek = jan4.getDay() === 0 ? 6 : jan4.getDay() - 1; // Adjusting Sunday (0) to 6, others to 0-5
    jan4.setDate(jan4.getDate() - dayOfWeek);
    return jan4.toISOString().split('T')[0];
}

console.log(getFirstMondayOfYear(2025));
Enter fullscreen mode Exit fullscreen mode

Using Built-in Methods in Some Languages

Some programming languages offer built-in methods to get the Monday of the first ISO week directly.

Python (Using isocalendar)

from datetime import date

def get_first_monday_of_year(year):
    return date.fromisocalendar(year, 1, 1)  # Year, ISO week 1, Monday (1)

print(get_first_monday_of_year(2025))
Enter fullscreen mode Exit fullscreen mode

Java (Using java.time API)

import java.time.LocalDate;
import java.time.temporal.WeekFields;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        LocalDate firstMonday = LocalDate.of(2025, 1, 1)
            .with(WeekFields.ISO.weekOfYear(), 1)
            .with(WeekFields.ISO.dayOfWeek(), 1);
        System.out.println(firstMonday);
    }
}
Enter fullscreen mode Exit fullscreen mode

C# (.NET) Using ISOWeek

using System;
using System.Globalization;

class Program {
    static void Main() {
        var firstMonday = ISOWeek.ToDateTime(2025, 1, DayOfWeek.Monday);
        Console.WriteLine(firstMonday.ToString("yyyy-MM-dd"));
    }
}
Enter fullscreen mode Exit fullscreen mode

Example

For 2025:

  • January 4, 2025, is a Saturday (day 5 if Monday=0).

  • Subtract 5 days from January 4:

    • January 4 - 5 days = Monday, December 30, 2024.

So, the first Monday of the first ISO week of 2025 is December 30, 2024.

To find the first Monday of the year, anchor to January 4th and count backward to the nearest Monday. This simple approach works consistently across different programming languages.

Top comments (0)