Developers of other programming languages will be confused when they use Go's time formatting method for the first time. Why is it not the familiar yyyy, MM, dd, but a special year, month, and day number?
First look at the correct use method
// 代码
s := time.Now().Format("2006-01-02 15:04:05.999999")
fmt.Printf("%s\n", s)
// 结果(正确)
2022-06-14 23:21:48.655781
What if the later time is casually entered? (Actually I copied the output above)
// 代码
s := time.Now().Format("2022-06-14 23:21:48.655781")
fmt.Printf("%s\n", s)
// 结果(出乎意料)
141414-22-627 1411:146:278.61818786
Since we have to follow other people's norms, how can we easily and quickly remember this magical number?
There is such a record in the official documentation and source code:
// These are predefined layouts for use in Time.Format and time.Parse.
// The reference time used in these layouts is the specific time stamp:
// 01/02 03:04:05PM '06 -0700
In fact, it is 3:00 (afternoon) on January 2nd, 4 minutes, 5 seconds, 6 years, and 7 time zones ( Cui Jian: 1234567… )
Our daily habits are the year, month and day, which brings a little trouble to memory, regardless of the time zone: 612345.
Remember? Write a few to try?
// 代码
s := time.Now().Format("2006年01月02日 15时04分05秒999毫秒")
fmt.Printf("%s\n", s)
// 输出(发现了吗?除了毫秒都正确!)
2022年06月14日 23时46分45秒999毫秒
// 经过测试,发现毫秒前面必须有:,或者.,使用9或者0都可以
time.Now().Format("2006年01月02日 15时04分05秒.000毫秒")
// 代码(整个奇怪的形状,月日时秒不补0)
s := time.Now().Format("2006/1/2 3:04:5")
fmt.Printf("%s\n", s)
// 输出
2022/6/14 11:53:2
Various details still have to look at the documentation:
// Year: "2006" "06"
// Month: "Jan" "January"
// Textual day of the week: "Mon" "Monday"
// Numeric day of the month: "2" "_2" "02"
// Numeric day of the year: "__2" "002"
// Hour: "15" "3" "03" (PM or AM)
// Minute: "4" "04"
// Second: "5" "05"
// AM/PM mark: "PM"
After reading so much, have you lost your studies? If you don't know how to use it, then package a yyyyMMdd in the way you are used to. Go simply uses the numbers 2006, 01, and 02 as yyyMMdd...
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。