dongshu4221 2018-10-10 13:53
浏览 126
已采纳

使用时间包的解析方法将mongo db字段的值“ date”解析为golang

I've created_date column as a string and value of it like 2018-10-04 15:42:19.000404667 +0000 UTC m=+103.387519062 which I got from mongo db columns and now I inserted it into mysql table and of course it is of string type. Now the problem is that I can't parse it and formatted it as well, here I tried with below code to parse but can't get solution.

tm, err := time.Parse("2006-02-01", "2018-10-04 15:42:19.000404667 +0000 UTC m=+103.387519062")
if err != nil {
    fmt.Println(err)
}

And it printed with some errors like :

parsing time "2018-10-04 15:42:19.000404667 +0000 UTC m=+103.387519062" as "2006-01-02T15:04:05.999999999Z07:00": cannot parse " 15:42:19.000404667 +0000 UTC m=+103.387519062" as "T"

I tried with different const of time package but still digging into it, what I'm missing here or am I doing parse with wrong date type.

Thanks.

  • 写回答

1条回答 默认 最新

  • dpl9717 2018-10-10 14:08
    关注

    Unless the m=... part is known to be constant (in which case you should include it in your format string as-is), you will have to strip it from the input string before passing it to time.Parse(). There is no way to tell time.Parse() that it should ignore a part of the input (except if it is always the same string).

    NOTE you should provide a complete time specifier as the format, not just "2006-02-01", because if you don't, the library will extend it in its own way to include hours, minutes and seconds - which isn't necessarily the way you want it (in your case it automatically added T15:04:05.999999999Z07:00 - but your input data contains a space after the date, not T, so it won't match). In your case (judging by your example input data), the format string should be like this: 2006-02-01 15:04:05.000000000 -0700 MST

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容