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_Time - Fatal编程技术网

Go 转到自定义时间

Go 转到自定义时间,go,time,Go,Time,我试图用自定义格式解析日期-ddmmmyyyy。例如: 15may1997 dd 15 mmm may yyyy 1997 这是我的代码: const customFormat = "02jan2006" t, err := time.Parse(customFormat, "15may1997") if err != nil { fmt.Println(err) } 这是输出: parsing time "15may1997" as "02jan2006": cannot pa

我试图用自定义格式解析日期-
ddmmmyyyy
。例如:

15may1997
dd 15 
mmm may 
yyyy 1997
这是我的代码:

const customFormat = "02jan2006"
t, err := time.Parse(customFormat, "15may1997")
if err != nil {
    fmt.Println(err)
}
这是输出:

parsing time "15may1997" as "02jan2006": cannot parse "may1997" as "jan"
只有在更改日期时,解析才会成功:

t, err := time.Parse(customFormat, "15jan2006")
我试着阅读了越来越多的文章,但不知道如何自定义我的格式

我做错了什么


谢谢。

Golang time。Parse根据布局解析格式化字符串并返回其表示的时间值。定义格式的布局区分大小写,并且具有固定的预定义值

因此,您需要将customFormat=“02jan2006”更新为“02jan2006”

下面是datetime不同部分的区分大小写的占位符列表

--------------- + ------------ +
Type            | Placeholder  |
--------------- + ------------ +
Year            | 2006         |
Year            | 06           | 
Month           | 01           |
Month           | 1            |
Month           | Jan          |
Month           | January      |
Day             | 02           |
Day             | 2            |
Week day        | Mon          |
Week day        | Monday       |
Hours           | 03           |
Hours           | 3            |
Hours           | 15           |
Minutes         | 04           |
Minutes         | 4            |
Seconds         | 05           | 
Seconds         | 5            |
AM or PM        | PM           | 
Miliseconds     | .000         |
Microseconds    | .000000      |
Nanoseconds     | .000000000   |
Timezone offset | -0700        |
Timezone offset | -07:00       |   
Timezone offset | Z0700        |  
Timezone offset | Z07:00       |   
Timezone        | MST          |
--------------- + ------------ +
识别的星期日格式为“周一”和“周一”。识别的月份格式为“一月”和“一月”。即,使用
Jan
而不是
Jan
。你完全正确:)谢谢mkoprita