1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-13 01:02:28 +08:00

feat: add NowDateOrTime

This commit is contained in:
dudaodong
2023-07-26 15:03:59 +08:00
parent 31e8b12674
commit 326e7881a6
4 changed files with 110 additions and 0 deletions

View File

@@ -50,6 +50,7 @@ func init() {
"yyyy": "2006",
"mm": "01",
"hh:mm:ss": "15:04:05",
"hh:mm": "15:04",
"mm:ss": "04:05",
}
}
@@ -281,3 +282,23 @@ func DayOfYear(t time.Time) int {
func IsWeekend(t time.Time) bool {
return time.Saturday == t.Weekday() || time.Sunday == t.Weekday()
}
// NowDateOrTime return current datetime with specific format and timezone.
// Play: todo
func NowDateOrTime(format string, timezone ...string) string {
tf := timeFormat[format]
if tf == "" {
return ""
}
if timezone != nil && timezone[0] != "" {
loc, err := time.LoadLocation(timezone[0])
if err != nil {
return ""
}
return time.Now().In(loc).Format(tf)
}
return time.Now().Format(tf)
}