How to get the last day of current month in Go

1 Answer

0 votes
package main
    
import (
    "fmt"
    "time"
)

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

    firstDayOfMonth := time.Date(currentYear, currentMonth, 1, 0, 0, 0, 0, now.Location())
    lastDayOfMonth := firstDayOfMonth.AddDate(0, 1, -1)

    fmt.Println(lastDayOfMonth)
    fmt.Println(lastDayOfMonth.Day())
}
 
 
   
   
/*
run:
   
2020-08-31 00:00:00 +0000 UTC
31
   
*/

 



answered Aug 27, 2020 by avibootz

Related questions

1 answer 187 views
1 answer 214 views
1 answer 259 views
2 answers 230 views
1 answer 249 views
1 answer 167 views
1 answer 170 views
170 views asked Aug 13, 2020 by avibootz
...