Use Date for date formatting in Golang (Java style)

  • 2020-10-31 21:47:11
  • OfStack

This article introduces the use of Date for date formatting in Golang and shares the details as follows:

Github
https://github.com/noogo/date

Date

Date is a 1 date package based on the time wrapper. With this package, you can quickly create a date, get the timestamp, milliseconds, and most important date formatting, and you can continue to use all the functions under the time package (except time.Foramt (string). You can quickly create an Date object by:

Now() WithTime(t time.Time) WithTimestamp(timestamp int64) WithMillisecond(millisecond int64) WithDate(year, month, date, hour, minute, second int)

Note: You can get it via ES29en. Format(String... bool) method to format the date, the date format is in accordance with the Java style, without the Golang unconventional formatting method, which adds great convenience to our use of date format, the following question Java date format reference table:

字母 日期或时间元素 表示 示例
G Era 标志符 Text AD
y Year 1996; 96
M 年中的月份 Month July; Jul; 07
w 年中的周数 Number 27
W 月份中的周数 umber 2
D 年中的天数 Number 189
d 月份中的天数 umber 10
F 月份中的星期 umber 2
E 星期中的天数 ext Tuesday; Tue v
a Am/pm 标记 Text PM
H 1天中的小时数(0-23) umber 0
k 1天中的小时数(1-24) umber 24
K am/pm 中的小时数(0-11) umber 0
h am/pm 中的小时数(1-12) umber 12
m 小时中的分钟数 umber 30
s 分钟中的秒数 umber 55
S 毫秒数 Number 978
z 时区 General time zone Pacific Standard Time; PST; GMT-08:00
Z 时区 RFC 822 time zone -0800

start

Get Date


go get -u github.com/noogo/date

Using Date


// get date
d:=date.Now()
//d:=date.WithTime(time.Now())
//d:=date.WithTimestamp(1586448000)
//d:=date.WithMillisecond(1586448000000)
//d:=date.WithDate(2020,04,29,0,0,0)
// get milliseconds
//milliseconds:=date.Millisecond()
// get timestamp
//timestamp:=date.Timestamp()
// date format
ret,err:=d.Format("yyyy-MM-dd HH:mm:ss EEEE",true)
if err!=nil{
  log.Fatalln(err)
}
fmt.Println(ret)

The results

[

2020-04-29 00:13:12 Week 3

]

Formatting instructions

G: Reserved fields. Formatting is not supported Year: When the consecutive number of y is less than 4, the year after the abbreviation will be displayed. For example, 2008 will be formatted as 08 Month: When the consecutive number of M is greater than 3, the English word month will be displayed; if it is equal to 3, the English word abbreviation will be displayed; otherwise, the digit month will be displayed, and the digit number will not be filled with 0. If the current time is January 1, 2020, 08:08:08, then the minutes after mm formatting will be 08, and the minutes after mmm formatting will be 008, and so on If Date Format (string,... The second parameter in bool) passes true, which stands for Chinese mode. This parameter controls am/pm and the number of days of the week, which will be formatted into morning/afternoon and Monday 1 format.

Formatting parameter result

Ream: the current date is 2008-08-18 18:28:38.888

layout result
y 08
yy 08
yyy 08
yyyy 2008
yyyyy 2008
M 08
MM 08
MMM Aug
MMMM August
MMMMM August
w 34
ww 34
www 034
wwww 0034
wwwww 00034
W 4
WW 04
WWW 004
WWWW 0004
WWWWW 00004
D 231
DD 231
DDD 231
DDDD 0231
DDDDD 00231
d 18
dd 18
ddd 018
dddd 0018
ddddd 00018
F 3
FF 03
FFF 003
FFFF 0003
FFFFF 00003
E 星期1(chinese)
EE 星期1(chinese)
EEE 星期1(chinese)
EEEE 星期1(chinese)
EEEEE 星期1(chinese)
a 下午(chinese)
aa 下午(chinese)
aaa 下午(chinese)
aaaa 下午(chinese)
aaaaa 下午(chinese)
E 1(standard)
EE 01(standard)
EEE Mon(standard)
EEEE Monday(standard)
EEEEE Monday(standard)
a PM(standard)
aa PM(standard)
aaa PM(standard)
aaaa PM(standard)
aaaaa PM(standard)
H 18
HH 18
HHH 018
HHHH 0018
HHHHH 00018
k 18
kk 18
kkk 018
kkkk 0018
kkkkk 00018
K 6
KK 06
KKK 006
KKKK 0006
KKKKK 00006
h 6
hh 06
hhh 006
hhhh 0006
hhhhh 00006
m 28
mm 28
mmm 028
mmmm 0028
mmmmm 00028
s 38
ss 38
sss 038
ssss 0038
sssss 00038
S 888
SS 888
SSS 888
SSSS 0888
SSSSS 00888
z CST
zz CST
zzz CST
zzzz CST
zzzzz CST
Z +0800
ZZ +0800
ZZZ +0800
ZZZZ +0800
ZZZZZ +0800


Related articles: