Home >Backend Development >Golang >How to Get a Date Range from a Week Number in Go?

How to Get a Date Range from a Week Number in Go?

Barbara Streisand
Barbara StreisandOriginal
2024-12-08 07:52:12971browse

How to Get a Date Range from a Week Number in Go?

Date Range by Week Number in Golang

Introduction

Determining the date range corresponding to a given week number can be a common requirement in many applications. While Golang provides a convenient function to obtain the week number, it lacks a built-in mechanism to retrieve the corresponding date range. This article presents a practical Golang solution to address this issue.

Custom Function Implementation

To establish the date range of a specific week, we need to perform the following steps:

  1. Align to Monday: As Time.ISOWeek() aligns with Monday, we start by setting the current time to the first Monday of the year.
  2. Find Week Number: Calculate the week number of this time using Time.ISOWeek().
  3. Adjust for Week Difference: Determine the difference between the target week number and the current week number and adjust the time accordingly.

Based on these principles, we can create a custom function called WeekStart() as follows:

func WeekStart(year, week int) time.Time {
    // Start from middle of the year
    t := time.Date(year, 7, 1, 0, 0, 0, 0, time.UTC)

    // Align to Monday
    if t.Weekday() == time.Sunday {
        t = t.AddDate(0, 0, -6)
    } else {
        t = t.AddDate(0, 0, -int(t.Weekday())+1)
    }

    // Adjust for week difference
    y, w := t.ISOWeek()
    t = t.AddDate(0, 0, (week-w)*7)

    return t
}

Usage and Example

To utilize this function, pass in the year and desired week number as arguments:

fmt.Println(WeekStart(2018, 1)) // Output: 2018-01-01 00:00:00 +0000 UTC
fmt.Println(WeekStart(2018, 2)) // Output: 2018-01-08 00:00:00 +0000 UTC

Retrieving the Last Day of the Week

If we need both the first and last day of the week, we can extend the function to return the end date:

func WeekRange(year, week int) (start, end time.Time) {
    start = WeekStart(year, week)
    end = start.AddDate(0, 0, 6)
    return
}

This expanded function enables the following usage:

fmt.Println(WeekRange(2018, 1)) // Output: 2018-01-01 00:00:00 +0000 UTC 2018-01-07 00:00:00 +0000 UTC
fmt.Println(WeekRange(2018, 2)) // Output: 2018-01-08 00:00:00 +0000 UTC 2018-01-14 00:00:00 +0000 UTC

This custom implementation empowers you to effortlessly retrieve the date range corresponding to a given week number, making it an invaluable tool for calendar manipulations and data analysis in Golang applications.

The above is the detailed content of How to Get a Date Range from a Week Number in Go?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn