Help us understand the problem. What is going on with this article?

【Go】Unix時刻と日時の相互変換

サンプルコード

package main

import (
    "fmt"
    "time"
)

func main() {
    // 現在時刻を取得
    now := time.Now()
    fmt.Println(now) // => 2019-09-16 22:56:39.564794 +0900 JST m=+0.000207404

    // Unix時刻を取得
    unix := now.Unix()
    fmt.Println(unix) // => 1568642199

    // Unix時刻を日時に変換
    datetimeByUnix := time.Unix(unix, 0)
    fmt.Println(datetimeByUnix) // => 2019-09-16 22:56:39 +0900 JST

    // Unix時刻をミリ秒まで取得
    unixNano := now.UnixNano()
    fmt.Println(unixNano) // => 1568642199564794000

    // Unix時刻をミリ秒まで日時に変換
    sec := unixNano / 1000000000
    nsec := unixNano % 1000000000
    datetimeByUnixNano := time.Unix(sec, nsec)
    fmt.Println(datetimeByUnixNano) // => 2019-09-16 22:56:39.564794 +0900 JST
}

参考

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away