6
3

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で日付(Month)

Last updated at Posted at 2015-05-16

golangで日付を作る際のMonthのメモ。

整数直書きはOK.

package main

import (
	"fmt"
	"time"
)

func main() {
	d := time.Date(2015, 7, 1, 0, 0, 0, 0, time.UTC)
	fmt.Println(d) // 2015-07-01 00:00:00 +0000 UTC
}

int型の変数に入れて渡すとtime.Month型で渡せと言われる。

	m := 7
	d := time.Date(2015, m, 1, 0, 0, 0, 0, time.UTC)
	fmt.Println(d) // エラー cannot use m (type int) as type time.Month in argument to time.Date

time.Month型にCastして渡す。

	m := 7
	d := time.Date(2015, time.Month(m), 1, 0, 0, 0, 0, time.UTC)
	fmt.Println(d) // 2015-07-01 00:00:00 +0000 UTC

time.Month型を直接書くとこんなん。

	d := time.Date(2015, time.July, 1, 0, 0, 0, 0, time.UTC)
	fmt.Println(d) // 2015-07-01 00:00:00 +0000 UTC

ソース見たらこんな感じで定義されてる。

// A Month specifies a month of the year (January = 1, ...).
    77	type Month int
    78	
    79	const (
    80		January Month = 1 + iota
    81		February
    82		March
    83		April
    84		May
    85		June
    86		July
    87		August
    88		September
    89		October
    90		November
    91		December
    92	)

6
3
0

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
6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?