9
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2014-12-11

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

コード

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 は ここ

参考

9
6
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?