在Go中如何将当前时间格式化为字符串?

huangapple go评论118阅读模式
英文:

Get current time as formatted string in Go?

问题

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

英文:

What's the best way to get the current timestamp in Go and convert to string? I need both date and time in eg. YYYYMMDDhhmmss format.

答案1

得分: 176

使用time.Now()函数和time.Format()方法。

t := time.Now()
fmt.Println(t.Format("20060102150405"))

打印出20110504111515,或者至少几分钟前是这样的。(我在东部夏令时。)在时间包中定义了几个预定义的时间格式的常量

如果你更喜欢使用UTC而不是本地时区,可以使用time.Now().UTC()

英文:

Use the time.Now() function and the time.Format() method.

t := time.Now()
fmt.Println(t.Format("20060102150405"))

prints out 20110504111515, or at least it did a few minutes ago. (I'm on Eastern Daylight Time.) There are several pre-defined time formats in the constants defined in the time package.

You can use time.Now().UTC() if you'd rather have UTC than your local time zone.

答案2

得分: 86

所有其他的回答对于从谷歌来寻找“go中的时间戳”这个问题的人来说都是非常误导的!YYYYMMDDhhmmss不是一个“时间戳”。

要在go中获取一个日期的“时间戳”(从1970年1月开始的秒数),正确的函数是Time.Unix(),它确实返回一个整数。

英文:

All the other response are very miss-leading for somebody coming from google and looking for "timestamp in go"! YYYYMMDDhhmmss is not a "timestamp".

To get the "timestamp" of a date in go (number of seconds from january 1970), the correct function is Time.Unix(), and it really return an integer

答案3

得分: 76

为了提高可读性,最好在time包中使用RFC常量(我认为)

import "fmt"
import "time"

func main() {
    fmt.Println(time.Now().Format(time.RFC850))
}
英文:

For readability, best to use the RFC constants in the time package (me thinks)

import "fmt" 
import "time"

func main() {
	fmt.Println(time.Now().Format(time.RFC850))
}

答案4

得分: 36

使用 time.Now()time.Format() 函数(因为 Go 1.0.3 中不再存在 time.LocalTime() 函数)

t := time.Now()
fmt.Println(t.Format("20060102150405"))

在线演示(在 playground 中将日期固定在过去,不用在意)

英文:

Use the time.Now() and time.Format() functions (as time.LocalTime() doesn't exist anymore as of Go 1.0.3)

t := time.Now()
fmt.Println(t.Format("20060102150405"))

Online demo (with date fixed in the past in the playground, never mind)

答案5

得分: 10

在这篇文章中可以找到更多信息:在golang中以不同格式获取当前日期和时间

这是一些不同格式的示例:

package main

import (
    "fmt"
    "time"
)

func main() {
    currentTime := time.Now()
    fmt.Println("当前时间字符串:", 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 {特殊字符}:", currentTime.Format("2006#01#02"))
    fmt.Println("YYYY-MM-DD hh:mm:ss:", currentTime.Format("2006-01-02 15:04:05"))
    fmt.Println("带微秒的时间:", currentTime.Format("2006-01-02 15:04:05.000000"))
    fmt.Println("带纳秒的时间:", currentTime.Format("2006-01-02 15:04:05.000000000"))
    fmt.Println("短数字月份:", currentTime.Format("2006-1-02"))
    fmt.Println("长月份:", currentTime.Format("2006-January-02"))
    fmt.Println("短月份:", currentTime.Format("2006-Jan-02"))
    fmt.Println("短年份:", currentTime.Format("06-Jan-02"))
    fmt.Println("长星期几:", currentTime.Format("2006-01-02 15:04:05 Monday"))
    fmt.Println("短星期几:", currentTime.Format("2006-01-02 Mon"))
    fmt.Println("短日期:", currentTime.Format("Mon 2006-01-2"))
    fmt.Println("短时分秒:", currentTime.Format("2006-01-02 3:4:5"))
    fmt.Println("短时分秒:", currentTime.Format("2006-01-02 3:4:5 PM"))
    fmt.Println("短时分秒:", currentTime.Format("2006-01-02 3:4:5 pm"))
}

输出结果为:

当前时间字符串: 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 {特殊字符}: 2017#07#04
YYYY-MM-DD hh:mm:ss: 2017-07-04 00:47:20
带微秒的时间: 2017-07-04 00:47:20.142475
带纳秒的时间: 2017-07-04 00:47:20.142475100
短数字月份: 2017-7-04
长月份: 2017-July-04
短月份: 2017-Jul-04
短年份: 17-Jul-04
长星期几: 2017-07-04 00:47:20 Tuesday
短星期几: 2017-07-04 Tue
短日期: Tue 2017-07-4
短时分秒: 2017-07-04 12:47:20
短时分秒: 2017-07-04 12:47:20 AM
短时分秒: 2017-07-04 12:47:20 am
英文:

Find more info in this post: Get current date and time in various format in golang

This is a taste of the different formats that you'll find:

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"))
}

The output is:

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

答案6

得分: 3

https://golang.org/src/time/format.go 指定了时间解析的格式。小时使用15,分钟使用04,秒使用05

日期解析中,月份使用11JanJanuary,日期使用02MonMonday,年份使用2006,时区使用MST

但你也可以使用这个简单的布局:"Mon Jan 2 15:04:05 MST 2006"

    const layout = "Mon Jan 2 15:04:05 MST 2006"
    userTimeString := "Fri Dec 6 13:05:05 CET 2019"

	t, _ := time.Parse(layout, userTimeString)
	fmt.Println("Server: ", t.Format(time.RFC850))
    //Server:  Friday, 06-Dec-19 13:05:05 CET

	mumbai, _ := time.LoadLocation("Asia/Kolkata")
	mumbaiTime := t.In(mumbai)
	fmt.Println("Mumbai: ", mumbaiTime.Format(time.RFC850))
    //Mumbai:  Friday, 06-Dec-19 18:35:05 IST

DEMO

英文:

https://golang.org/src/time/format.go specified
For parsing time 15 is used for Hours, 04 is used for minutes, 05 for seconds.

For parsing Date 11, Jan, January is for months, 02, Mon, Monday for Day of the month, 2006 for year and of course MST for zone

But you can use this layout as well, which I find very simple. "Mon Jan 2 15:04:05 MST 2006"

    const layout = "Mon Jan 2 15:04:05 MST 2006"
    userTimeString := "Fri Dec 6 13:05:05 CET 2019"

	t, _ := time.Parse(layout, userTimeString)
	fmt.Println("Server: ", t.Format(time.RFC850))
    //Server:  Friday, 06-Dec-19 13:05:05 CET

	mumbai, _ := time.LoadLocation("Asia/Kolkata")
	mumbaiTime := t.In(mumbai)
	fmt.Println("Mumbai: ", mumbaiTime.Format(time.RFC850))
    //Mumbai:  Friday, 06-Dec-19 18:35:05 IST

DEMO

答案7

得分: 2

您可以简单地使用像***strconv.Itoa(int(time.Now().Unix()))***这样的方式。

英文:

You can simply use like strconv.Itoa(int(time.Now().Unix()))

答案8

得分: 1

作为对@Bactisme回答的回应,获取当前时间戳(以毫秒为单位,例如)的方法如下:

msec := time.Now().UnixNano() / 1000000

资源:https://gobyexample.com/epoch

英文:

As an echo to @Bactisme's response, the way one would go about retrieving the current timestamp (in milliseconds, for example) is:

msec := time.Now().UnixNano() / 1000000

Resource: https://gobyexample.com/epoch

答案9

得分: -4

为了回答确切的问题:

import "github.com/golang/protobuf/ptypes"
Timestamp, _ = ptypes.TimestampProto(time.Now())
英文:

To answer the exact question:

import "github.com/golang/protobuf/ptypes"
Timestamp, _ = ptypes.TimestampProto(time.Now())

huangapple
  • 本文由 发表于 2011年5月4日 23:00:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/5885486.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定