如何获取Go中的当前时间戳?

bri*_*noh 118 timestamp go

在Go中获取当前时间戳并转换为字符串的最佳方法是什么?我需要例如日期和时间.YYYYMMDDhhmmss格式.

nmi*_*els 135

使用time.Now()功能和time.Format()方法.

t := time.Now()
fmt.Println(t.Format("20060102150405"))
Run Code Online (Sandbox Code Playgroud)

打印出来20110504111515,或者至少是几分钟前打印出来的.(我在东部夏令时.)时间包中定义的常量中有几种预定义的时间格式.

time.Now().UTC()如果您希望使用UTC而不是当地时区,则可以使用.

  • 我非常喜欢'Kitchen`常量(`="3:04 PM"`) (15认同)
  • @brianoh:见http://golang.org/pkg/time/#Constants现在是时间"01/02 03:04:05 PM '06 -0700"因为每个组件都有不同的数字(1,2,3,等等,它可以从数字中确定你想要的组件. (7认同)
  • 谢谢你的信息.如何将时间从包传递知道"20060102150405",正是我们是路过的,因为它是不是在时间包预先定义的常量之一?时间包(20060102150405)中该日期和时间的重要性是什么?这对我来说似乎有点古怪,但只要它有效,我想只要我们在编码时没有出错就无所谓.我猜他们认为不适合为该格式提供常量,并匹配字符串模式. (3认同)

Bac*_*sme 66

对于那些来自谷歌并寻找"时间戳"的人来说,所有其他反应都非常缺乏领先!YYYYMMDDhhmmss不是"时间戳".

要获取日期的"时间戳"(从1970年1月开始的秒数),正确的函数是.Unix(),它实际上返回一个整数

  • 我同意; 问题应该标题为"当前日期"而不是"当前时间戳" (8认同)

小智 54

为了便于阅读,最好在时间包中使用RFC常量(我认为)

import "fmt" 
import "time"

func main() {
    fmt.Println(time.Now().Format(time.RFC850))
}
Run Code Online (Sandbox Code Playgroud)

  • 这是如何产生YYYYMMDDhhmmss的? (4认同)
  • 我个人更喜欢:`time.Now().Format(time.RFC3339)`,示例输出:`2021-08-19T13:52:51+03:00` (3认同)
  • RFC850 生成 `2009 年 11 月 10 日星期二 23:00:00 UTC` `RFC3339 = "2006-01-02T15:04:05Z07:00"` https://play.golang.org/p/XmobwWSz5pN https:/ /golang.org/pkg/time/ (2认同)

Del*_*ace 32

使用time.Now()time.Format()函数(如time.LocalTime()不作为围棋1.0.3的存在了)

t := time.Now()
fmt.Println(t.Format("20060102150405"))
Run Code Online (Sandbox Code Playgroud)

在线演示 (过去在操场上固定日期,没关系)

  • 你可以简单地将它用作这样的字符串:``s:="实际时间是:"+ time.Now().String()``` (6认同)

Edu*_*íaz 13

在这篇文章中查找更多信息:在 golang 中以各种格式获取当前日期和时间

这是您会发现的不同格式的体验:

package main

import (
    "fmt"
    "time"
)

func main() {
    currentTime := time.Now()
    fmt.Println("Current Time in String: ", currentTime.String())
    fmt.Println("MM-DD-YYYY : ", currentTime.Format("01-02-2006"))
    fmt.Println("YYYY-MM-DD : ", currentTime.Format("2006-01-02"))
    fmt.Println("YYYY.MM.DD : ", currentTime.Format("2006.01.02 15:04:05"))
    fmt.Println("YYYY#MM#DD {Special Character} : ", currentTime.Format("2006#01#02"))
    fmt.Println("YYYY-MM-DD hh:mm:ss : ", currentTime.Format("2006-01-02 15:04:05"))
    fmt.Println("Time with MicroSeconds: ", currentTime.Format("2006-01-02 15:04:05.000000"))
    fmt.Println("Time with NanoSeconds: ", currentTime.Format("2006-01-02 15:04:05.000000000"))
    fmt.Println("ShortNum Month : ", currentTime.Format("2006-1-02"))
    fmt.Println("LongMonth : ", currentTime.Format("2006-January-02"))
    fmt.Println("ShortMonth : ", currentTime.Format("2006-Jan-02"))
    fmt.Println("ShortYear : ", currentTime.Format("06-Jan-02"))
    fmt.Println("LongWeekDay : ", currentTime.Format("2006-01-02 15:04:05 Monday"))
    fmt.Println("ShortWeek Day : ", currentTime.Format("2006-01-02 Mon"))
    fmt.Println("ShortDay : ", currentTime.Format("Mon 2006-01-2"))
    fmt.Println("Short Hour Minute Second: ", currentTime.Format("2006-01-02 3:4:5"))
    fmt.Println("Short Hour Minute Second: ", currentTime.Format("2006-01-02 3:4:5 PM"))
    fmt.Println("Short Hour Minute Second: ", currentTime.Format("2006-01-02 3:4:5 pm"))
}
Run Code Online (Sandbox Code Playgroud)

输出是:

Current Time in String:  2017-07-04 00:47:20.1424751 +0530 IST
MM-DD-YYYY :  07-04-2017
YYYY-MM-DD :  2017-07-04
YYYY.MM.DD :  2017.07.04 00:47:20
YYYY#MM#DD {Special Character} :  2017#07#04
YYYY-MM-DD hh:mm:ss :  2017-07-04 00:47:20
Time with MicroSeconds:  2017-07-04 00:47:20.142475
Time with NanoSeconds:  2017-07-04 00:47:20.142475100
ShortNum Month :  2017-7-04
LongMonth :  2017-July-04
ShortMonth :  2017-Jul-04
ShortYear :  17-Jul-04
LongWeekDay :  2017-07-04 00:47:20 Tuesday
ShortWeek Day :  2017-07-04 Tue
ShortDay :  Tue 2017-07-4
Short Hour Minute Second:  2017-07-04 12:47:20
Short Hour Minute Second:  2017-07-04 12:47:20 AM
Short Hour Minute Second:  2017-07-04 12:47:20 am
Run Code Online (Sandbox Code Playgroud)

  • 请以代码形式发布代码,而不是以图片形式发布。 (2认同)