package main
import (
"fmt"
"time"
)
func main () {
t := time.Now()
fmt.Println(t)
t1 := time.Now().Format("2006-01-02 15:04:05")
fmt.Println(t1)
fmt.Println(time.Now().Unix())
str_time := time.Unix(time.Now().Unix(),0).Format("2006-01-02 15:04:05")
fmt.Println(str_time)
the_time, err := time.Parse("2006-01-02 15:04:05", "2014-01-08 09:04:41")
if err == nil {
unix_time := the_time.Unix()
fmt.Println(unix_time)
}
}
2017-10-10 02:00:20.950450595 +0000 UTC
2017-10-10 02:00:20
1507600820
2017-10-10 02:00:20
1389171881
更多格式化
package main
import (
"fmt"
"time"
)
const (
date = "2006-01-02"
shortdate = "06-01-02"
times = "15:04:05"
shorttime = "15:04"
datetime = "2006-01-02 15:04:05"
newdatetime = "2006/01/02 15~04~05"
newtime = "15~04~05"
)
func main () {
t := time.Now()
fmt.Println(t)
t1 := time.Now().Format("2006-01-02 15:04:05")
fmt.Println(t1)
fmt.Println(time.Now().Unix())
str_time := time.Unix(time.Now().Unix(),0).Format("2006-01-02 15:04:05")
fmt.Println(str_time)
the_time, err := time.Parse("2006-01-02 15:04:05", "2014-01-08 09:04:41")
if err == nil {
unix_time := the_time.Unix()
fmt.Println(unix_time)
}
fmt.Println("***************************")
thisdate := "2014-03-17 14:55:06"
timeformatdate, _ := time.Parse(datetime, thisdate)
convdate := timeformatdate.Format(date)
convshortdate := timeformatdate.Format(shortdate)
convtime := timeformatdate.Format(times)
convshorttime := timeformatdate.Format(shorttime)
convnewdatetime := timeformatdate.Format(newdatetime)
convnewtime := timeformatdate.Format(newtime)
fmt.Println(timeformatdate)
fmt.Println(convdate)
fmt.Println(convshortdate)
fmt.Println(convtime)
fmt.Println(convshorttime)
fmt.Println(convnewdatetime)
fmt.Println(convnewtime)
}
2017-10-10 02:29:20.691900785 +0000 UTC
2017-10-10 02:29:20
1507602560
2017-10-10 02:29:20
1389171881
***************************
2014-03-17 14:55:06 +0000 UTC
2014-03-17
14-03-17
14:55:06
14:55
2014/03/17 14~55~06
14~55~06
fmt.Println(time.Now().Unix())
tm2, _ := time.Parse("20060102", "20180104")
fmt.Println(tm2.Unix())
tm3, _ := time.Parse("01/02/2006", "01/04/2018")
fmt.Println(tm3.Unix())
输出:
1515121674
1515024000
1515024000
fmt.Println(time.Now().Unix())
tm2, _ := time.Parse("20060102", "20180101")
fmt.Println(tm2.Unix())
tm3, _ := time.Parse("01/02/2006", "01/01/2018")
fmt.Println(tm3.Unix())
days := (time.Now().Unix() - tm2.Unix())/86400
fmt.Println(days)
1515121865
1514764800
1514764800
4
thisdate := "20171221150609"
timeformatdate,_ := time.Parse("20060102150405", thisdate)
fmt.Println(timeformatdate)
convdate := timeformatdate.Format("2006-01-02 15:04:05")
fmt.Println(convdate)
2017-12-21 15:06:09 +0000 UTC
2017-12-21 15:06:09
//计算当前天的前一天日期,目录计算相关操作时有用
nTime := time.Now()
yesTime := nTime.AddDate(0,0,-1)
var currentDay string = nTime.Format("20060102")
var yesterday string = yesTime.Format("20060102")
fmt.Println(currentDay)
fmt.Println(yesterday)
yesTime := nTime.AddDate(0,0,-1)
var currentDay string = nTime.Format("20060102")
var yesterday string = yesTime.Format("20060102")
fmt.Println(currentDay)
fmt.Println(yesterday)
//输出
20180322
20180321