博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
开始日期和时间格式
阅读量:2503 次
发布时间:2019-05-11

本文共 2617 字,大约阅读时间需要 8 分钟。

In Go, the current time can be determined using time.Now(), provided by the .

在Go中,可以使用提供的time.Now()确定当前时间。

t := time.Now()

This will print the system local time. Want UTC instead? append .UTC():

这将打印系统本地时间。 需要UTC吗? 附加.UTC()

The time can be formatted using the time.Format() method:

可以使用time.Format()方法格式化时间:

t := time.Now().UTC()

Want the timestamp?

需要时间戳吗?

t := time.Now().Unix()

使用自定义格式 (Use a custom format)

An example of formatting with a custom format is:

使用自定义格式进行格式化的示例如下:

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

That 2006-01-02 15:04:05 string looks strange, isn’t it? It’s not like it’s 2006 now! But it will print (at the time of writing) 2017-01-16 12:53:51

2006-01-02 15:04:05字符串看起来很奇怪,不是吗? 现在不像是2006年! 但是它将打印(在撰写本文时) 2017-01-16 12:53:51

If you’re new to Go, this will sound very strange.

如果您不熟悉Go,这听起来奇怪。

Here’s the explanation: Go’s time formatting is unique and different than what you would do in other languages. Instead of having a conventional format to print the date, Go uses the reference date 20060102150405 which seems meaningless but actually has a reason, as it’s 1 2 3 4 5 6 in the Posix date command:

解释如下:Go的时间格式是独特的,并且与其他语言的格式不同。 Go没有使用常规格式来打印日期,而是使用了参考日期20060102150405 ,该日期似乎没有意义,但实际上是有原因的,因为Posix date命令中的date1 2 3 4 5 6

Mon Jan 2 15:04:05 -0700 MST 20060   1   2  3  4  5              6

The timezone in the middle would have been the 7 (not really sure why they didn’t pick Mon Jan 2 03:04:05 -0600 MST 2007, by the way)

中间的时区应该是7 (顺便说一下,不确定他们为什么不选择Mon Jan 2 03:04:05 -0600 MST 2007 )

Interesting historical reference:

有趣的历史参考: :

使用格式常量 (Use a format constants)

Go provides in the time package some handy constants for commonly used formats:

Go在time包中为常用格式提供了一些方便的常量:

const (        ANSIC       = "Mon Jan _2 15:04:05 2006"        UnixDate    = "Mon Jan _2 15:04:05 MST 2006"        RubyDate    = "Mon Jan 02 15:04:05 -0700 2006"        RFC822      = "02 Jan 06 15:04 MST"        RFC822Z     = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone        RFC850      = "Monday, 02-Jan-06 15:04:05 MST"        RFC1123     = "Mon, 02 Jan 2006 15:04:05 MST"        RFC1123Z    = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone        RFC3339     = "2006-01-02T15:04:05Z07:00"        RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"        Kitchen     = "3:04PM"        // Handy time stamps.        Stamp      = "Jan _2 15:04:05"        StampMilli = "Jan _2 15:04:05.000"        StampMicro = "Jan _2 15:04:05.000000"        StampNano  = "Jan _2 15:04:05.000000000")

You can use them like this:

您可以像这样使用它们:

t := time.Now()fmt.Println(t.Format(time.ANSIC))

翻译自:

转载地址:http://vlqgb.baihongyu.com/

你可能感兴趣的文章
JSP九大内置对象及四个作用域
查看>>
OCAC公告
查看>>
Modernizr.js介绍与使用
查看>>
ConnectionString 属性尚未初始化
查看>>
解决Spring MVC无法接收AJAX使用PUT与DELETE请求传输的内容
查看>>
数据结构-栈 C和C++的实现
查看>>
发布功能完成
查看>>
C#获取客服端ip和用户名
查看>>
Asp.net MVC 之ActionResult
查看>>
jQuery Easy UI (适应屏幕分辨率大小)布局(Layout)
查看>>
ES6学习之字符串的扩展
查看>>
[SDOI2014]旅行
查看>>
scala学习笔记-Actor(19)
查看>>
ADT+NDK+OpenCV 环境部署
查看>>
GDB调试实用命令
查看>>
Java 浮点运算
查看>>
线程安全
查看>>
Centos7安装tomcat8
查看>>
MySQL基本命令和常用数据库对象
查看>>
poj 1222 EXTENDED LIGHTS OUT(位运算+枚举)
查看>>