go-date-time-conver

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package main

import(
"fmt"
"time"
)

func main() {
datetime := "2015-01-01 00:00:00" //待转化为时间戳的字符串

//日期转化为时间戳
timeLayout := "2006-01-02 15:04:05" //转化所需模板
loc, _ := time.LoadLocation("Local") //获取时区
tmp, _ := time.ParseInLocation(timeLayout, datetime, loc)
timestamp := tmp.Unix() //转化为时间戳 类型是int64
fmt.Println(timestamp)

//时间戳转化为日期
datetime = time.Unix(timestamp, 0).Format(timeLayout)
fmt.Println(datetime)
}

输出

1
2
1420041600
2015-01-01 00:00:00