dthh5038 2017-07-05 11:10
浏览 105

golang,time.Parse(“ 11:22 PM”)我可以进行数学运算的东西

I am importing a lot of fields of the format:

09:02 AM
10:02 AM
12:30 PM
04:10 PM
04:50 PM
05:30 PM

I would like to convert the fields into something I can do arithmetic on. For example, do a count down to when the event Occurs. Thus, saving the field in microseconds... or even seconds. I have been trying to get the time.Parse to work... no joy.

fmt.Println(time.Parse("hh:mm", m.Feed.Entry[i].GsxA100Time.T))

returns...

0001-01-01 00:00:00 +0000 UTC parsing time "07:50 PM" as "hh:mm": cannot parse "07:50 PM" as "hh:mm"

any suggestions?

  • 写回答

3条回答 默认 最新

  • dpwle46882 2017-07-05 11:36
    关注

    The layout string for time.Parse does not handle the "hh:mm" format. In your case, the layout string would rather be "03:04 PM" as you can see in the documentation.

    To get a time.Duration after parsing the string, you can substract your time with a reference time, in your case I would assume "12:00 AM".

    Working example:

    package main
    
    import (
        "fmt"
        "time"
    )
    
    func main() {
        ref, _ := time.Parse("03:04 PM", "12:00 AM")
        t, err := time.Parse("03:04 PM", "11:22 PM")
        if err != nil {
            panic(err)
        }
    
        fmt.Println(t.Sub(ref).Seconds())
    }
    

    Output:

    84120

    Playground

    评论

报告相同问题?

悬赏问题

  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了