go date

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)

//输出
20180322
20180321


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值