doupoji3856 2015-12-23 15:01 采纳率: 100%
浏览 987
已采纳

将MySQL日期时间字符串转换为time.Time格式

I just cant manage to parse an SQL datetime (MySQL) value into a time.Time value. I cant find the layout fitting sql datetime. And also not really understand how this works.

I do imagine I'am not the first struggling with this, though i cant really find how I should make this work.

Input:

2015-12-23 00:00:00

Desired output:

1450825200

Code

time, err := time.Parse(time.SomeSqlDateTimeLayout, "2015-12-23 00:00:00")
timestamp := time.Unix()
  • 写回答

2条回答 默认 最新

  • douyong4842 2015-12-23 16:05
    关注

    You can create your own tie format for parsing, if one does not exist in standard library.

    package main
    
    import (
        "fmt"
        "time"
    )
    
    func main() {
        layout := "2006-01-02 15:04:05"
        str := "2015-12-23 00:00:00"
        t, err := time.Parse(layout, str)
    
        if err != nil {
            fmt.Println(err)
        }
        fmt.Println(t.Unix())
    }
    

    Output

    1450828800
    

    I do not know were official documentation for time format is, but you can find it here, from line 64.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程