当前位置:网站首页>go语言time.Format的坑

go语言time.Format的坑

2022-06-09 11:36:00 .番茄炒蛋

问题描述

Format以后输出的时间与now是不一样的

package main

import (
	"fmt"
	"time"
)

func main() {
    
	now := time.Now()
	fmt.Println(now)
	format := now.Format("2006-01-01 15:04:05")
	fmt.Println(format)
}

在这里插入图片描述

解决办法

package main

import (
	"fmt"
	"time"
)

func main() {
    
	now := time.Now()
	fmt.Println(now)
	format := now.Format("2006-01-02 15:04:05")
	fmt.Println(format)
}

在这里插入图片描述
使用time.Format格式化时间参数layout必须要传"2006-01-02 15:04:05",为什么呢?没有为什么,这就是最坑的地方,据说这个日期是GO语言的诞生时间,格式化时就必须要传这个时间,传其他的时间都会有问题.

原网站

版权声明
本文为[.番茄炒蛋]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_43135259/article/details/125193313