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

I am trying to get the first and last day of the current month. You can add days and hours, but not the month that I thought about subtracting one day from the next month to get the last day of this month. Something like that:

package main

import (
    "fmt"
    "time"
)

func main() {

    date := time.Now()
    nextMonth := date.Add(time.Month)
    LastDay := nextMonth.Add(-time.Hour * 24)

    fmt.Println(LastDay)

}
+12
source share
7 answers

Now you can use the library, it is really simple:

now.BeginningOfMonth()    // 2013-11-01 00:00:00 Fri
now.EndOfMonth()          // 2013-11-30 23:59:59.999999999 Sat

Please look here for details: https://github.com/jinzhu/now

+6
source

time.Month - , , Add . , , , , - . 24 , 23 .

, :

package main

import (
    "time"
    "fmt"
)

func main() {
    now := time.Now()
    currentYear, currentMonth, _ := now.Date()
    currentLocation := now.Location()

    firstOfMonth := time.Date(currentYear, currentMonth, 1, 0, 0, 0, 0, currentLocation)
    lastOfMonth := firstOfMonth.AddDate(0, 1, -1)

    fmt.Println(firstOfMonth)
    fmt.Println(lastOfMonth)
}

+21

@Apin , lib ( ).
: https://github.com/jinzhu/now/issues/13

:

t := time.Now()
firstday := time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, time.Local)
lastday := firstday.AddDate(0, 1, 0).Add(time.Nanosecond * -1)
+10

import "time" 

func Date

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

,

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

.

, , , , nsec . , 32 1 .

. , , 13 2011 . 2:15 , 6 2011 1:15 . , , , . , , , .

, loc .

. , ,

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)
}

:

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
+4

, AddDate(...) time.Time, , , time.Time :)

time.Time.AddDate(...) -

func BeginningOfMonth(date time.Time)  (time.Time) {
    return date.AddDate(0, 0, -date.Day() + 1)
}

func EndOfMonth(date time.Time) (time.Time) {
    return date.AddDate(0, 1, -date.Day())
}

today := time.Now() / , , - eom := EndOfMonth(today) .

, , , .

, , - https://play.golang.org/p/DxnGuqh6g4k

+1

Goutils func. goutils

beginningOfTMonth:=time.TimeBeginningOfMonth(t)
endOfTMonth:=time.TimeEndOfMonth(t)


beginningOfTWeek:=time.TimeBeginningOfWeek(t)
endOfTWeek:=time.TimeEndOfWeek(t)
0

I would do it like this:

// LastDayOfMonth returns 28-31 - the last day in the month of the time object
// passed in to the function
func LastDayOfMonth(t time.Time) int {
    firstDay := time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, time.UTC)
    lastDay := firstDay.AddDate(0, 1, 0).Add(-time.Nanosecond)
    return lastDay.Day()
}

and

0
source

All Articles