Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go中的日期转换_Go - Fatal编程技术网

Go中的日期转换

Go中的日期转换,go,Go,我有下面的代码。从请求中将日期值作为字符串类型,并尝试转换为时间类型。但是,我对格式有一些问题 type LeanData struct { Start_Date time.Time } const dateLayout = "01-02-2006" startdate := request.FormValue("startdate") if len(strings.TrimSpace(startdate)) > 0 { sdate, err := time.Par

我有下面的代码。从请求中将日期值作为字符串类型,并尝试转换为时间类型。但是,我对格式有一些问题

type LeanData struct {
    Start_Date time.Time
}   
const dateLayout = "01-02-2006"

startdate := request.FormValue("startdate")
if len(strings.TrimSpace(startdate)) > 0 {
    sdate, err := time.Parse(dateLayout, startdate)
}
fmt.Println("startdate", startdate)
fmt.Println("sdate", sdate)
我有以下输出

startdate 02-03-2016
sdate 2016-02-03 00:00:00 +0000 UTC
在这里,我正在进行转换,因为开始日期的类型是time.time。 我想将其转换为2016-02-03,但不是2016-02-03 00:00:00+0000 UTC。 另外,如果请求中的值为nil/empty,如何将空值分配给开始日期


有人能告诉我如何实现这一点吗?

time.Parse
返回类型为
time.time
的对象,其中包含日期和时间信息。Go中没有只包含日期信息的类型。但是,在格式化日期时,可以忽略日期的时间部分,例如:

fmt.Println("sdate", sdate.Format("2006-01-02"))
将打印:

sdate 2016-02-03
要初始化空时间,只需将其声明为:

var sdate time.Time

看起来转换很好。如果只需要日期部分而不需要时间,请使用或。