7
5

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.

Goの日付フォーマットでミリ秒の末尾0を省略しない書式

Posted at

time - The Go Programming Language
に日付フォーマットがいくつか定義してあります。

RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"

はナノ秒まで出るので、これを参考に"2006-01-02T15:04:05.999Z07:00"とすればミリ秒になったのですが、末尾のゼロは取り除かれます。つまり0.120秒の場合は.12になります。

書式フォーマットで9の代わりに0を指定して"2006-01-02T15:04:05.000Z07:00"とすれば、.120になります。ログの書式ではこちらのほうが私は好みです。

package main

import (
	"fmt"
	"time"
)

func main() {
	t := time.Date(2013, 9, 2, 1, 55, 20, 120e6, time.Local)
	fmt.Println(t.Format("2006-01-02T15:04:05.999Z07:00"))
	fmt.Println(t.Format("2006-01-02T15:04:05.000Z07:00"))
}
2013-09-02T01:55:20.12Z
2013-09-02T01:55:20.120Z

と出力されます。

http://play.golang.org/p/Dvhhb7GLgR
で試せます。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?