0
0

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.

time.Timeで日付だけにする(タイムゾーン付)

0
Last updated at Posted at 2019-05-17

現在時刻を元に0:00にしたり、正午にしたり

タイムゾーンを考慮せずUTCでよければTruncate()を使う方法がありますが、
JSTで、わかりやすく、指定した時間にするにはどうしたらいいのか。
以下の方法がわかりやすいと思いました。

t := time.Now()
tz, _ := time.LoadLocation("Asia/Tokyo")

yy, mm, dd := t.Date()
today := time.Date(yy, mm, dd, 0, 0, 0, 0, tz)
todayNoon := time.Date(yy, mm, dd, 12, 0, 0, 0, tz)

log.Println(today.Format("2006-01-02 15:04:05")) // 2019-05-17 00:00:00
log.Println(todayNoon.Format("2006-01-02 15:04:05")) // 2019-05-17 12:00:00

参考にした

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?