LoginSignup
0
0

More than 3 years have passed since last update.

GOのtimeライブラリ

Posted at

インポート

import "time"

日付取得

// 現在日時の取得
t = time.Now()
fmt.Println("'time.Now': ", t)

// TimeStampから日時の取得
t_by_unix := time.Unix(1561895883, 0)
fmt.Println("'time.Unix': ", t_by_unix)

// 指定日時の取得
t_by_date := time.Date(2019, time.Month(6), 23, 1, 30, 30, 0, l)
fmt.Println("'time.Date': ", t_by_date)

日付表示

// UTCタイムゾーンで日付表示
t_by_utc := t.UTC()
fmt.Println("'t.UTC': ", t_by_utc)

// ローカルタイムゾーンで日付表示
t_by_local := t.Local()
fmt.Println("'t.Local': ", t_by_local)

// 指定タイムゾーンで日付表示
t_in := t.In(time.UTC)
fmt.Println("'t.In': ", t_in)

// 指定フォーマットで日付表示
fmt.Println("t.Format", t.Format(time.RFC3339))

日付計算

// 1年1ケ月1日後の日付を求める
t_new := t.AddDate(1, 1, 1)
fmt.Println("'t.AddDate': ", t_new)

// 日付比較
is_after := t.After(t_new)
fmt.Println("'t.After': ", is_after)

JSONへエンコーディング

// JSONへエンコーディング
t_byte, err := t.MarshalJSON()
fmt.Println("'t.MarshalJSON': ", t_byte, err)

// JSONからデコーディング
var t_un time.Time
err = t_un.UnmarshalJSON(t_byte)
fmt.Println("'t_un.UnmarshalJSON': ", t_un, err)

その他

// 処理を一時停止
d_second := time.Second
time.Sleep(d_second)
0
0
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
0
0