Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
6
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

posted at

updated at

メモ: golangで月末日を取得する方法

これで正しいのか、あんまり自信ない。

コード

getlastdayofmonth.go
package main

import (
    "fmt"
    "time"
)

func main() {
    loc, _ := time.LoadLocation("Asia/Tokyo")
    t := time.Date(2014, time.March, 1, 0, 0, 0, 0, loc).AddDate(0, 0, -1)
    fmt.Println(t)
    fmt.Println(t.Day())
}

上記では、2014 年 3 月の月初から 1 日分を引き算している。

timeDate() 内の loc 部分の指定は別に time.UTC でも問題ない…と思う。

出力
2014-02-28 00:00:00 +0900 JST
28

Go Playground は ここ

参考

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
6
Help us understand the problem. What are the problem?