如何将日期解析为time.Time类型的数据?

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

Go: How to parse only date to time.Time?

问题

我想将日期值解析为time.Time类型。
例如,我有一个日期的格式为2016-03-31,我想要解析它,类似于time.Parse(FORMAT, "2016-03-31")

但是它总是失败。

正确的格式字符串是什么,以便只解析具有这种格式的日期?

以下是一个示例代码,也可以在playground上找到:https://play.golang.org/p/0MNLr9emZd

package main

import (
	"fmt"
	"time"
)

var dateToParse = "2016-03-31"

func main() {
	format := "2006-01-02"
	parseDate(format)
}

func parseDate(format string) {
	t, err := time.Parse(format, dateToParse)
	if err != nil {
		fmt.Println("Format:", format)
		fmt.Println(err)
		fmt.Println("")
		return
	}
	fmt.Println("Works Format:", format)
	fmt.Println(t)
	fmt.Println("")
}

输出结果如下:

Format: 2006-01-02
parsing time "2016-03-31" as "2006-01-02": cannot parse "-31" as "2"
英文:

I want to parse only date value to time.Time.
For example I have date in this format: 2016-03-31, and I want to parse it, like: time.Parse(FORMAT, "2016-03-31").

But it always fail.

What is the correct format string to use to parse only date with this format?

I have the code below as example, it is on playground also: https://play.golang.org/p/0MNLr9emZd

package main

import (
	"fmt"
	"time"
)

var dateToParse = "2016-03-31"

func main() {
	format := "2006-12-01"
	parseDate(format)
}

func parseDate(format string) {
	t, err := time.Parse(format, dateToParse)
	if err != nil {
		fmt.Println("Format:", format)
		fmt.Println(err)
		fmt.Println("")
		return
	}
	fmt.Println("Works Format:", format)
	fmt.Println(t)
	fmt.Println("")
}

The output is this:

Format: 2006-12-01
parsing time "2016-03-31" as "2006-12-01": cannot parse "-31" as "2"

答案1

得分: 5

时间包

这些是在Time.Format和Time.Parse中使用的预定义布局。
布局中使用的参考时间是特定的时间:

Mon Jan 2 15:04:05 MST 2006

这是Unix时间1136239445。由于MST是GMT-0700,参考时间可以被认为是

01/02 03:04:05PM '06 -0700

要定义自己的格式,请写下参考时间按您的方式进行格式化的样子;参考常量的值,如ANSIC、StampMicro或Kitchen,可以作为示例。

使用format := "2006-01-02"表示yyyy-mm-dd。

英文:

> Package time
>
> These are predefined layouts for use in Time.Format and Time.Parse.
> The reference time used in the layouts is the specific time:
>
> Mon Jan 2 15:04:05 MST 2006
>
> which is Unix time 1136239445. Since MST is GMT-0700, the reference
> time can be thought of as
>
> 01/02 03:04:05PM '06 -0700
>
> To define your own format, write down what the reference time would
> look like formatted your way; see the values of constants like ANSIC,
> StampMicro or Kitchen for examples.

Use format := "2006-01-02" for yyyy-mm-dd.

答案2

得分: 3

根据Go 1.20发布说明

新的时间布局常量DateTime、DateOnly和TimeOnly为调查公共Go源代码中使用最广泛的三个布局字符串提供了名称。

    DateTime   = "2006-01-02 15:04:05"
	DateOnly   = "2006-01-02"
	TimeOnly   = "15:04:05"

根据提案time: add DateTime, DateOnly, TimeOnly format constants提交,新的格式DateOnly = "2006-01-02"将在Go的下一个版本(1.20)中添加到format.go中。

time.Parse(time.DateOnly, dateToParse)
英文:

Per Go 1.20 release note

> The new time layout constants DateTime, DateOnly, and TimeOnly provide names for three of the most common layout strings used in a survey of public Go source code.

    DateTime   = "2006-01-02 15:04:05"
	DateOnly   = "2006-01-02"
	TimeOnly   = "15:04:05"

The new format DateOnly = "2006-01-02" of format.go will be added in the Go next release (1.20) per proposal time: add DateTime, DateOnly, TimeOnly format constants and commit

time.Parse(time.DateOnly, dateToParse)

huangapple
  • 本文由 发表于 2017年7月28日 19:35:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/45372490.html
匿名

发表评论

匿名网友

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

确定