运维开发网

golang时间处理

运维开发网 https://www.qedev.com 2020-03-13 08:31 出处:网络 作者:运维开发网整理
golang 时间处理 package main import ( "fmt" "time" ) const ( date = "2006-01-02" shortdate = "06-01-02" times = "15:04:02" shorttime = "15:04" datetime = "2006-01-02 15:04:02"

golang 时间处理

package main

import (
	"fmt"
	"time"
)

const (
	date        = "2006-01-02"
	shortdate   = "06-01-02"
	times       = "15:04:02"
	shorttime   = "15:04"
	datetime    = "2006-01-02 15:04:02"
	newdatetime = "2006/01/02 15~04~02"
	newtime     = "15~04~02"
)

func main() {
	thisdate := "2014-03-17 14:55:06"
	timeformatdate, _ := time.Parse(datetime, thisdate)
	fmt.Println(timeformatdate)
	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(convdate)
	fmt.Println(convshortdate)
	fmt.Println(convtime)
	fmt.Println(convshorttime)
	fmt.Println(convnewdatetime)
	fmt.Println(convnewtime)
}

格式化当前时间

lasttime, _ := time.Now().Format("2006-01-02 15:04:05")
0

精彩评论

暂无评论...
验证码 换一张
取 消