技术笔记技术笔记
首页
前言
  • CSS
  • JQuery
  • JavaScript
  • PHP
  • GoLang
Mysql
运维
Linux
  • Sublime Text 3
  • PhpStorm
  • Markdown
  • Git
  • Vim
  • MAC命令
  • Eclipse插件
  • Content-Type
  • 公务员职级体系
  • Eclipse插件
  • Content-Type
  • Web工具
  • 技术资源分享
首页
前言
  • CSS
  • JQuery
  • JavaScript
  • PHP
  • GoLang
Mysql
运维
Linux
  • Sublime Text 3
  • PhpStorm
  • Markdown
  • Git
  • Vim
  • MAC命令
  • Eclipse插件
  • Content-Type
  • 公务员职级体系
  • Eclipse插件
  • Content-Type
  • Web工具
  • 技术资源分享
  • GoLang

    • 介绍
    • 常用命令
    • 时间函数
    • JSON函数
    • 常见问题

时间函数

字符串转时间戳

本地时间和0时区的差距

str := "2017-03-04 12:11:45"
res, err := time.Parse("2006-01-02 15:04:05", str)
if err == nil {
	fmt.Println(res.Unix())
}

本地时间

str := "2017-03-04 12:11:45"
res2, err := time.ParseInLocation("2006-01-02 15:04:05", str, time.Local)
if err == nil {
	fmt.Println(res2.Unix())
}

字符串转时间戳另一种方法

the_time := time.Date(2014, 1, 7, 5, 50, 4, 0, time.Local)
unix_time := the_time.Unix()
fmt.Println(unix_time)

获取时间戳

timestamp := time.Now().Unix()
fmt.Println(timestamp)

格式化为字符串,tm为Time类型

tm := time.Unix(timestamp, 0)
fmt.Println(tm.Format("2006-01-02 03:04:05 PM"))
fmt.Println(tm.Format("02/01/2006 15:04:05 PM"))

当前格式化时间

fmt.Println(time.Now().Format("2006-01-02 15:04:05"))

time.Time转成unix

time.Time.Unix()

unix转成time.Time

time.Unix(time.Time, 0) 
最近更新: 2025/2/23 23:16
Contributors: huangyanfu, hqdxhyf
Prev
常用命令
Next
JSON函数