英文:
convert YYYYMMDD string to a valid date in Go
问题
我想将字符串"20101011"
转换为有效的日期(2010-10-11
),但是无法弄清楚如何做到这一点。
我尝试了以下方法:
now := time.Now()
date := now.Format("20101011")
和
date, _ := time.Parse("20101011", "20101011")
但是两种方法都没有成功。
英文:
I want to convert a string "20101011"
to a valid date (2010-10-11
), but could not figure our how to do it.
I tried:
now := time.Now()
date := now.Format("20101011")
and
date, _ := time.Parse("20101011", "20101011")
neither one worked.
答案1
得分: 13
> 时间包
>
> import "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
> 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
> RFC3339 = "2006-01-02T15:04:05Z07:00"
> RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
> Kitchen = "3:04PM"
> // 方便的时间戳。
> 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"
> )
>
> 这些是用于 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 和 Parse 方法可以对一般的时间值应用相同的转换。
使用 time
格式字符串 "20060102"
表示 YYYYMMDD
。使用 time
格式字符串 "2006-01-02"
表示 YYYY-MM-DD
。
例如:
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now()
fmt.Println(now)
date := now.Format("20060102")
fmt.Println(date)
date = now.Format("2006-01-02")
fmt.Println(date)
date2, err := time.Parse("20060102", "20101011")
if err == nil {
fmt.Println(date2)
}
}
输出:
2009-11-10 23:00:00 +0000 UTC
20091110
2009-11-10
2010-10-11 00:00:00 +0000 UTC
英文:
> Package time
>
> import "time"
>
> Constants
>
> 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"
> )
>
> 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. The model is to demonstrate what
> the reference time looks like so that the Format and Parse methods can
> apply the same transformation to a general time value.
Use the time
format string "20060102"
for YYYYMMDD
. Use the time
format string "2006-01-02"
for YYYY-MM-DD
.
For example,
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now()
fmt.Println(now)
date := now.Format("20060102")
fmt.Println(date)
date = now.Format("2006-01-02")
fmt.Println(date)
date2, err := time.Parse("20060102", "20101011")
if err == nil {
fmt.Println(date2)
}
}
Output:
2009-11-10 23:00:00 +0000 UTC
20091110
2009-11-10
2010-10-11 00:00:00 +0000 UTC
答案2
得分: 1
你可以执行以下操作:
dateStr := "20210131" // 日期以 'String' 数据类型表示
dateValue, _ := time.Parse("20060102", dateStr) // 将 'String' 转换为 'Time' 数据类型
fmt.Println(dateValue) // 输出:2021-01-31 00:00:00 +0000 UTC
dateStr = dateValue.Format("2006-01-02") // Format 方法将返回一个按指定格式(YYYY-MM-DD)的 'string'
fmt.Println(dateStr) // 输出:2021-01-31
英文:
You can do the following:
dateStr := "20210131" // date in 'String' data type
dateValue, _ := time.Parse("20060102", dateStr) // convert 'String' to 'Time' data type
fmt.Println(dateValue) // output: 2021-01-31 00:00:00 +0000 UTC
dateStr = dateValue.Format("2006-01-02") // Format return a 'string' in your specified layout (YYYY-MM-DD)
fmt.Println(dateStr) // Output: 2021-01-31
答案3
得分: 0
如果您不介意使用另一个库,Google的civil
包有一个很好的ParseDate()
函数,可以跳过中间的time.Time
转换。可以像这样使用它:
func ParseDate(s string) (Date, error) {
date, err := civil.ParseDate(s)
return Date{date}, err
}
英文:
If you don't mind another library, Google's civil
package has a nice ParseDate()
which skips the interim time.Time
conversion. It can be used like so:
func ParseDate(s string) (Date, error) {
date, err := civil.ParseDate(s)
return Date{date}, err
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论