LoginSignup
5
5

More than 5 years have passed since last update.

ちょっと変わったGoの日付フォーマット

Posted at

多数の言語で日付をフォーマットするとき、yyyy-MM-ddようなパラメータを指定していますが、Goの場合はちょっと違った。

例えばRubyでは

Time.now.strftime('%Y-%m-%d %H:%M:%S')

Goではこのようになります:

  now = time.Now()
  fmt.Println(now.Format("2006-01-02 15:04:05.000000000"))

上記に2006のような具体的な数字は決まりで、ルールがあります:

年 :06,2006
月 :1,01,Jan,January
日 :2,02,_2
時間:3,03,15
分 :4,04
秒 :5,05
週 :Mon,Monday
TimeZone:-07,-0700,Z0700,Z07:00,-07:00,MST

例えば今日(2014/02/21)でを下記のように計算すると:

  // output: Fri (short)
  fmt.Println(now.Format("Mon"))

  // output: Friday (full)
  fmt.Println(now.Format("Monday"))

  // output: 14 (short)
  fmt.Println(now.Format("06"))

  // output: 2014 (full)
  fmt.Println(now.Format("2006"))

  // output: 2 (short)
  fmt.Println(now.Format("1"))

  // output: 02 (full)
  fmt.Println(now.Format("01"))

参考:
http://golang.org/pkg/time/#Parse

5
5
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
5
5