Socket
Book a DemoInstallSign in
Socket

github.com/nelsonkti/date

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/nelsonkti/date

Source
Go
Version
v0.0.0-20200524082255-954f149e8f2d
Version published
Created
Source

Date

noogo-2020 noogo-2020

Date是一个基于time包装的一个日期包,通过此包可以快速创建日期、获取时间戳、毫秒数及最重要的日期格式化,另外你还可以继续使用time包下的所有函数(除time.Foramt(string)外)你可以通过以下方法快速创建一个Date对象:

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

Note:你可以通过Date.Format(String,...bool)方法来对日期进行格式化,日期格式化是按照Java风格实现的,免去了Golang中非常规的格式化方法,这对我们使用日期格式化增加了很大的便利,以下问Java日期格式化参考表:

字母日期或时间元素表示示例
GEra 标志符TextAD
yYear1996; 96
M年中的月份MonthJuly; Jul; 07
w年中的周数Number27
W月份中的周数Number2
D年中的天数Number189
d月份中的天数Number10
F月份中的星期Number2
E星期中的天数TextTuesday; Tue v
aAm/pm 标记TextPM
H一天中的小时数(0-23)Number0
k一天中的小时数(1-24)Number24
Kam/pm 中的小时数(0-11)Number0
ham/pm 中的小时数(1-12)Number12
m小时中的分钟数Number30
s分钟中的秒数Number55
S毫秒数Number978
z时区General time zonePacific Standard Time; PST; GMT-08:00
Z时区RFC 822 time zone-0800

开始

获取Date

go get -u github.com/noogo/date

使用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)

运行结果

2020-04-29 00:13:12 星期三

格式化说明

  • G:保留字段,不支持格式化

  • 年:当y的连续个数小于4时则显示缩写后的年,如2008,则会格式化为08

  • 月:当M的连续个数大于3时则显示英文单词月份,如果等于3则显示英文单词缩写,否则显示数字月份,位数不足用0填充。

  • 对于表格中表示类型为Number类型的按照统一规则显示对应数值,其余多余的格式化字符用0填充,假如当前时间为2020年1月1日,08时08分08秒,那么mm格式化后的分钟则为08mmm格式化后的分钟则为008依次类推

  • 如果Date.Format(string,...bool)中第二个参数传true,代表中文模式,此参数控制am/pm及星期数,对应会被格式化为上午/下午星期一格式。

格式化参结果

令:当前日期为2008-08-18 18:28:38.888

layoutresult
y08
yy08
yyy08
yyyy2008
yyyyy2008
M08
MM08
MMMAug
MMMMAugust
MMMMMAugust
w34
ww34
www034
wwww0034
wwwww00034
W4
WW04
WWW004
WWWW0004
WWWWW00004
D231
DD231
DDD231
DDDD0231
DDDDD00231
d18
dd18
ddd018
dddd0018
ddddd00018
F3
FF03
FFF003
FFFF0003
FFFFF00003
E星期一(chinese)
EE星期一(chinese)
EEE星期一(chinese)
EEEE星期一(chinese)
EEEEE星期一(chinese)
a下午(chinese)
aa下午(chinese)
aaa下午(chinese)
aaaa下午(chinese)
aaaaa下午(chinese)
E1(standard)
EE01(standard)
EEEMon(standard)
EEEEMonday(standard)
EEEEEMonday(standard)
aPM(standard)
aaPM(standard)
aaaPM(standard)
aaaaPM(standard)
aaaaaPM(standard)
H18
HH18
HHH018
HHHH0018
HHHHH00018
k18
kk18
kkk018
kkkk0018
kkkkk00018
K6
KK06
KKK006
KKKK0006
KKKKK00006
h6
hh06
hhh006
hhhh0006
hhhhh00006
m28
mm28
mmm028
mmmm0028
mmmmm00028
s38
ss38
sss038
ssss0038
sssss00038
S888
SS888
SSS888
SSSS0888
SSSSS00888
zCST
zzCST
zzzCST
zzzzCST
zzzzzCST
Z+0800
ZZ+0800
ZZZ+0800
ZZZZ+0800
ZZZZZ+0800

License

MIT License

Copyright (c) noogo-2020

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

FAQs

Package last updated on 24 May 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.