教程集 www.jiaochengji.com
教程集 >  脚本编程  >  C语言  >  正文 golang time包使用方法详解

golang time包使用方法详解

发布时间:2017-12-13   编辑:jiaochengji.com
教程集为您提供golang time包使用方法详解等资源,欢迎您收藏本站,我们将为您提供最新的golang time包使用方法详解资源
golang time包是一个时间包了,今天我们来看一些关于golang time包的使用例子,希望这篇文章能够帮助到各位.

golang time包提供了对时间的显示和操作的方法。

值的一提的是,go不像其它语言使用类似Ymd等字符来格式化时间,而是以下面这个时间模板为准:
Mon Jan 2 15:04:05 -0700 MST 2006

下面的例子提供了时间常见的格式化和显示操作:
package main
 
import (
 "time"
 "fmt"
)
func main()  {
 //当前时间戳
 fmt.Println(time.Now().Unix()) //1447682568
 
 //格式化当前时间
 fmt.Println(time.Now().Format("2006-01-02")) //2015-11-16
 
 //格式化时间戳
 fmt.Println(time.Unix(1447682568, 0).Format("2006-01-02 15:04:05")) //2015-11-16 22:02:48
 
 //时间字符串格式化成时间戳
 strTime := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
 fmt.Println(strTime) //2009-11-10 23:00:00 0000 UTC
 fmt.Println(strTime.Unix()) //1257894000
 
 the_time, _ := time.Parse("2006-01-02 15:04:05", "2014-01-08 09:04:41")
 fmt.Println(the_time.Unix())//1389171881
}

您可能感兴趣的文章:
golang time包使用方法详解
golang package time 用法详解
2020-10-16Go语言日期函数
gorm time.Time 使用钩子函数解决反序列化问题
PHP strtotime函数详细介绍
【Golang 基础】Go 语言的程序结构
GO语言中的“时间”
Golang 日期/时间包的使用
最常用的调试 golang 的 bug 以及性能问题的实践方法?
golang基础学习-base64使用

[关闭]
~ ~