Go 中 time.Parse 报错:year/month/day hour/minute/second out of range 时间格式化为什么是 2006-01-02 15:04:05?
1. 问题现象
在使用 Go
语言的 time.Parse
解析时间时遇到以下错误:
func main() {timeParse, err := time.Parse("2006-11-02 15:04:05", "2020-05-22 10:25:30")if err != nil {fmt.Println("time parse failed: ", err)}fmt.Printf("timeParse is %v, type is %T", timeParse, timeParse)
}
报错:
time parse failed: parsing time "2020-05-22 10:25:30": month out of range
timeParse is 0001-01-01 00:00:00 +0000 UTC, type is time.Time
2. 问题分析
查看源码发现有以下描述
Parse parses a formatted string and returns the time value it represents. The layout defines the format by showing how the reference time, defined to be Mon Jan 2 15:04:05 -0700 MST 2006 would be interpreted if it were the value; it serves as an example of the input format. The same interpretation will then be made to the input string.
layout
这个参数是被定义为 Mon Jan 2 15:04:05 -0700 MST 2006
3. 问题解决
我们修改 “2006-11-02 15:04:05” 为 “2006-01-02 15:04:05”
func main() {timeParse, err := time.Parse("2006-01-02 15:04:05", "2020-05-22 10:25:30")if err != nil {fmt.Println("time parse failed: ", err)}fmt.Printf("timeParse is %v, type is %T", timeParse, timeParse)// timeParse is 2020-05-22 10:25:30 +0000 UTC, type is time.Time
}
执行结果:
timeParse is 2020-05-22 10:25:30 +0000 UTC, type is time.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 zoneRFC850 = "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 zoneRFC3339 = "2006-01-02T15:04:05Z07:00"RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"Kitchen = "3:04PM"
)
Go 中 time.Parse 报错:year/month/day hour/minute/second out of range 时间格式化为什么是 2006-01-02 15:04:05?相关推荐
- 《一起学习rgbdSLAM》中g2o部分报错的问题
<一起学习rgbdSLAM>中g2o部分报错的问题 在高博的<一起学习rgbdSLAM>第六讲中使用到了g2o工具.但是由于版本问题,按照高博的写法来编写代码在编译过程中会报错 ...
- anconda安装后命令行中安装tensorflow报错
现象 anconda安装后命令行中安装tensorflow报错 pip install --upgrade --ignore-installed tensorflow-gpu Building wh ...
- K.O. -------- Eclipse中Maven的报错处理
----------------------siwuxie095 K.O. -------- Eclipse 中 Maven 的报错处理 1.报错一: A 'Maven Build' configur ...
- python无法启动该程序因为计算机中丢失_python报错:无法启动此程序,因为计算机中丢失...
原标题:python报错:无法启动此程序,因为计算机中丢失 python报错:无法启动此程序,因为计算机中丢失api-ms-win-crt-runtime-|1-1-0.dll api-ms-win- ...
- Navicat 远程连接docker容器中的mysql 报错1251 - Client does not support authentication protocol 解决办法
Navicat 远程连接docker容器中的mysql 报错1251 - Client does not support authentication protocol 解决办法 1).容器中登录my ...
- python无法启动此程序因为计算机中丢失_python报错:无法启动此程序,因为计算机中丢失...
原标题:python报错:无法启动此程序,因为计算机中丢失 python报错:无法启动此程序,因为计算机中丢失api-ms-win-crt-runtime-|1-1-0.dll api-ms-win- ...
- Eclipse中启动tomcat报错:A child container failed during start
我真的很崩溃,先是workspace崩了,费了好久重建的workspace,然后建立了一个小demo项目,tomcat中启动却报错,挑选其中比较重要的2条信息如下: A child container ...
- Eclipse和MyEclipse使用技巧--解决MyEclipse中的js报错的小方法
今天,下了个模版,但是导进去的时候发现js会报错.看了下其他都没有错误.而有一个js报错误,请原谅我有点红色强迫症,不能留一点红色 . 错误如下: Syntax error on token &quo ...
- python下载库报错_下载python中Crypto库报错:ModuleNotFoundError: No module named ‘Crypto’的解决...
下载python中Crypto库报错:ModuleNotFoundError: No module named 'Crypto'的解决 前言 最近在网上找了很多下载Crypto的方法,感觉作用都不算很 ...
最新文章
- loss函数为何选交叉熵
- SLAM:VSLAM简介之详细攻略
- Visual Studio 2017 15.5.0 正式发布 正式版下载
- TensorFlow 1.12.2 发布,修复 GIF 构造安全漏洞
- 计算机等级考试试题在线测试,计算机等级考试上机练习题.pdf
- malloc()与calloc区别
- input标签与图片按钮水平对齐的解决方法
- 证书 vivo_vivo秦飞:真假5G不存在 NSA模式手机未来仍可用
- hlw8012arduino代码_麻雀虽小五脏俱全-liteduino arduino开发板PCB文件
- 使用HTML语言和CSS开发商业站点_利用CSS3制作网页动画
- Android WiFi热点监听
- CSS3的新特性:css响应式多列布局、断字
- 博客页面添加动态小人,二次元人物 hexo live2d html人物公仔
- 新建Application 报错android.app.Application cannot be cast
- scrapy 爬取糗事百科段子篇章二(下载用户头像)
- 利用51单片机,矩阵键盘按键显示0--F在数码管
- 国产Linux系统UOS安装
- 数据库面试题(选择题)
- debian9.6安装和卸载新立得软件包管理器
- php 红宝石,红宝石-世界名贵宝石排行榜-天天排行网