golang 时间格式打印

package main

import (
	"fmt"
	"time"
)

const (
	// 2006-01-02 15:04:05为go的标准格式
	timeFormat1 = "2006-01-02 15:04:05"
	timeFormat2 = "2006-01-02-15-04-05"
	timeFormat3 = "2006-01-02 15:04:05.000" // 可以精确到毫秒ms
	// 非go的标准格式 打印的时间格式为未知时间
	timeFormat4 = "2022-01-02 10:04:05"
	timeFormat5 = "2022-01-02-10-04-05"
	timeFormat6 = "2022-01-02 10:04:05.000" // 可以精确到毫秒ms
)

func main() {
	// 获取当前时间
	now := time.Now()

	fmt.Printf("timeFormat1[%s] now[%s]\n", timeFormat1, now.Format(timeFormat1))
	fmt.Printf("timeFormat2[%s] now[%s]\n", timeFormat2, now.Format(timeFormat2))
	fmt.Printf("timeFormat3[%s] now[%s]\n", timeFormat3, now.Format(timeFormat3))
	fmt.Printf("timeFormat4[%s] now[%s]\n", timeFormat4, now.Format(timeFormat4))
	fmt.Printf("timeFormat5[%s] now[%s]\n", timeFormat5, now.Format(timeFormat5))
	fmt.Printf("timeFormat6[%s] now[%s]\n", timeFormat6, now.Format(timeFormat6))
}

运行结果如下:

timeFormat1[2006-01-02 15:04:05] now[2022-06-09 09:55:48]
timeFormat2[2006-01-02-15-04-05] now[2022-06-09-09-55-48]
timeFormat3[2006-01-02 15:04:05.000] now[2022-06-09 09:55:48.691]
#非go标准格式输出的时间不符合预期
timeFormat4[2022-01-02 10:04:05] now[9099-06-09 60:55:48]
timeFormat5[2022-01-02-10-04-05] now[9099-06-09-60-55-48]
timeFormat6[2022-01-02 10:04:05.000] now[9099-06-09 60:55:48.691]

注意:时间格式字符串一定得是go的诞生时间"2006-01-02 15:04:05",否则时间输出会超出你的想象。。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值