doujiao3998 2018-04-25 03:19
浏览 280
已采纳

在golang中将12小时格式转换为24小时格式时间

In golang I have not found any way to convert 12 hour format string time to 24 hour format as below:

07:05:45PM to 19:05:45

I have tried below using layout

layout := "Mon Jan 2 15:04:05 -0700 MST 2006"
/*
 * Write your code here.
 */
//layout := "3:04PM"
t,_ := time.Parse(layout,s)
fmt.Println(t)

Output is:

07:05:45PM

I have looked for answers similar to this but it is not helping everyone is using whole layout. I found answers in another language but not in go.

  • 写回答

2条回答 默认 最新

  • dongzhentiao2326 2018-04-25 04:10
    关注

    For example,

    package main
    
    import (
        "fmt"
        "time"
    )
    
    func main() {
        layout1 := "03:04:05PM"
        layout2 := "15:04:05"
        t, err := time.Parse(layout1, "07:05:45PM")
        if err != nil {
            fmt.Println(err)
            return
        }
        fmt.Println(t.Format(layout1))
        fmt.Println(t.Format(layout2))
    }
    

    Playground: https://play.golang.org/p/Ypn2-lEF_Zs

    Output:

    07:05:45PM
    19:05:45
    

    Reference: package time

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

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)