Get the first and last day of current month in Go/Golang?

后端 未结 8 1235
礼貌的吻别
礼貌的吻别 2021-02-01 04:07

I\'m trying to get the first and last day of the current month. You can add days and hours but not the month, which I was thinking of subtracting one day from the next month to

8条回答
  •  佛祖请我去吃肉
    2021-02-01 04:52

    Package time

    import "time" 
    

    func Date

    func Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time
    

    Date returns the Time corresponding to

    yyyy-mm-dd hh:mm:ss + nsec nanoseconds

    in the appropriate zone for that time in the given location.

    The month, day, hour, min, sec, and nsec values may be outside their usual ranges and will be normalized during the conversion. For example, October 32 converts to November 1.

    A daylight savings time transition skips or repeats times. For example, in the United States, March 13, 2011 2:15am never occurred, while November 6, 2011 1:15am occurred twice. In such cases, the choice of time zone, and therefore the time, is not well-defined. Date returns a time that is correct in one of the two zones involved in the transition, but it does not guarantee which.

    Date panics if loc is nil.

    Month and day values outside their usual ranges will be normalized. For example, for the first and last day of the month time interval,

    package main
    
    import (
        "fmt"
        "os"
        "time"
    )
    
    func monthInterval(t time.Time) (firstDay, lastDay time.Time) {
        y, m, _ := t.Date()
        loc := t.Location()
        firstDay = time.Date(y, m, 1, 0, 0, 0, 0, loc)
        lastDay = time.Date(y, m+1, 1, 0, 0, 0, -1, loc)
        return firstDay, lastDay
    }
    
    func main() {
        t := time.Now()
        fmt.Println(t.Round(0))
        first, last := monthInterval(t)
        fmt.Println(first)
        fmt.Println(last)
    
        dstLoc, err := time.LoadLocation("America/Los_Angeles")
        if err != nil {
            fmt.Fprintln(os.Stderr, err)
            return
        }
        // Sunday, March 12, 2017, 2:00:00 am to Sunday, March 12, 2017, 3:00:00 am
        dstStart := time.Date(2017, 03, 12, 2+1, 0, 0, 0, dstLoc)
        // Sunday, November 5, 2017, 2:00:00 am to Sunday, November 5, 2017, 1:00:00 am
        dstEnd := time.Date(2017, 11, 5, 2-1, 0, 0, 0, dstLoc)
        t = dstStart
        fmt.Println()
        fmt.Println(t)
        first, last = monthInterval(t)
        fmt.Println(first)
        fmt.Println(last)
        t = dstEnd.Add(time.Hour)
        fmt.Println()
        fmt.Println(t)
        first, last = monthInterval(t)
        fmt.Println(first)
        fmt.Println(last)
    }
    

    Output:

    2017-10-27 05:45:08.197312082 -0400 EDT
    2017-10-01 00:00:00 -0400 EDT
    2017-10-31 23:59:59.999999999 -0400 EDT
    
    2017-03-12 03:00:00 -0700 PDT
    2017-03-01 00:00:00 -0800 PST
    2017-03-31 23:59:59.999999999 -0700 PDT
    
    2017-11-05 01:00:00 -0800 PST
    2017-11-01 00:00:00 -0700 PDT
    2017-11-30 23:59:59.999999999 -0800 PST
    

提交回复
热议问题