douhua1890 2017-12-26 15:37
浏览 29

Go中的日期转换

I have below code. Having date value as string type from request and trying to convert to time type. But, I have some issue with format.

type LeanData struct {
    Start_Date time.Time
}   
const dateLayout = "01-02-2006"

startdate := request.FormValue("startdate")
if len(strings.TrimSpace(startdate)) > 0 {
    sdate, err := time.Parse(dateLayout, startdate)
}
fmt.Println("startdate", startdate)
fmt.Println("sdate", sdate)

I have below output.

startdate 02-03-2016
sdate 2016-02-03 00:00:00 +0000 UTC

Here, I am doing conversion because start date is of type time.Time. I want to convert it as 2016-02-03 but not with 2016-02-03 00:00:00 +0000 UTC. Also, How to assign empty value to start date if value from request is nil/empty.

Can someone let me know how to achieve this?

  • 写回答

1条回答 默认 最新

  • dongxiang3648 2017-12-26 15:52
    关注

    time.Parse returns an object of type time.Time which contains date and time information. There is no type in Go only containing date information. You can however ignore the time part of the date when formatting it, eg:

    fmt.Println("sdate", sdate.Format("2006-01-02"))
    

    which will print:

    sdate 2016-02-03
    

    To initialize an empty time, just declare it as:

    var sdate time.Time
    
    评论

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)