分享
Go时间格式化和类型互换操作
abcijkxyz · · 1429 次点击 · · 开始浏览这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。
获取本地时间
// get current timestamp
currentTime := time.Now().Local()
//print time
fmt.Println(currentTime)
指定格式的日期字符类型
// get current timestamp
currentTime := time.Now().Local()
//format Time, string type
newFormat := currentTime.Format("2006年01月02日 15:04:05.000")
fmt.Println(newFormat)
构造指定时间类型
//build Time 2016-02-17 23:59:59.999, DateTime type
myTime := time.Date(0162, time.February, 17, 23, 59, 59, 999, time.UTC)
//output the myTime
fmt.Println("MyTime:", myTime.Format("2006年01月02日 15:04:05.000"))
完整代码:
package main
import (
"fmt"
"time"
)
func FormatTime() {
// get current timestamp
currentTime := time.Now().Local()
//print time
fmt.Println(currentTime)
//format Time, string type
newFormat := currentTime.Format("2006年01月02日 15:04:05.000")
fmt.Println(newFormat)
//build Time 2016-02-17 23:59:59.999, DateTime type
myTime := time.Date(0162, time.February, 17, 23, 59, 59, 999, time.UTC)
//output the myTime
fmt.Println("MyTime:", myTime.Format("2006年01月02日 15:04:05.000"))
//current milliseconds
fmt.Println("milliseconds:", time.Now().UnixNano()/int64(time.Millisecond))
//TODO Changing time layout(form)
}
func main(){
FormatTime()
}
欢迎加入我的微信公众号
good!
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
关注微信1429 次点击
上一篇:Go语言为什么这么流行?
下一篇:Go包管理gpm简介
添加一条新回复
(您需要 后才能回复 没有账号 ?)
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码` - 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传
收入到我管理的专栏 新建专栏
获取本地时间
// get current timestamp
currentTime := time.Now().Local()
//print time
fmt.Println(currentTime)
指定格式的日期字符类型
// get current timestamp
currentTime := time.Now().Local()
//format Time, string type
newFormat := currentTime.Format("2006年01月02日 15:04:05.000")
fmt.Println(newFormat)
构造指定时间类型
//build Time 2016-02-17 23:59:59.999, DateTime type
myTime := time.Date(0162, time.February, 17, 23, 59, 59, 999, time.UTC)
//output the myTime
fmt.Println("MyTime:", myTime.Format("2006年01月02日 15:04:05.000"))
完整代码:
package main
import (
"fmt"
"time"
)
func FormatTime() {
// get current timestamp
currentTime := time.Now().Local()
//print time
fmt.Println(currentTime)
//format Time, string type
newFormat := currentTime.Format("2006年01月02日 15:04:05.000")
fmt.Println(newFormat)
//build Time 2016-02-17 23:59:59.999, DateTime type
myTime := time.Date(0162, time.February, 17, 23, 59, 59, 999, time.UTC)
//output the myTime
fmt.Println("MyTime:", myTime.Format("2006年01月02日 15:04:05.000"))
//current milliseconds
fmt.Println("milliseconds:", time.Now().UnixNano()/int64(time.Millisecond))
//TODO Changing time layout(form)
}
func main(){
FormatTime()
}
欢迎加入我的微信公众号
good!