diff --git a/datetime/datetime.go b/datetime/datetime.go index d3a24a5..3a2c87b 100644 --- a/datetime/datetime.go +++ b/datetime/datetime.go @@ -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) +} diff --git a/datetime/datetime_test.go b/datetime/datetime_test.go index ebedfa9..6c9d908 100644 --- a/datetime/datetime_test.go +++ b/datetime/datetime_test.go @@ -375,3 +375,22 @@ func TestIsWeekend(t *testing.T) { result2 := IsWeekend(date2) assert.Equal(false, result2) } + +func TestNowDateOrTime(t *testing.T) { + t.Parallel() + + formats := []string{ + "yyyy-mm-dd hh:mm:ss", + "yyyy-mm-dd", + "dd-mm-yy hh:mm:ss", + "yyyy/mm/dd hh:mm:ss", + "hh:mm:ss", + "yyyy/mm", + "yyyy-mm-dd hh", + } + + for i := 0; i < len(formats); i++ { + result := NowDateOrTime(formats[i], "UTC") + t.Log(result) + } +} diff --git a/docs/datetime.md b/docs/datetime.md index 4f03994..f63f63b 100644 --- a/docs/datetime.md +++ b/docs/datetime.md @@ -60,6 +60,7 @@ import ( - [BetweenSeconds](#BetweenSeconds) - [DayOfYear](#DayOfYear) - [IsWeekend](#IsWeekend) +- [NowDateOrTime](#NowDateOrTime)
@@ -1305,3 +1306,37 @@ func main() { // false } ``` + +### NowDateOrTime + +Return current datetime with specific format and timezone.
+ +Signature: + +```go +func NowDateOrTime(format string, timezone ...string) string +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/datetime" +) + +func main() { + result1 := datetime.NowDateOrTime("yyyy-mm-dd hh:mm:ss") + + result2 := datetime.NowDateOrTime("yyyy-mm-dd hh:mm:ss", "EST") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // 2023-07-26 15:01:30 + // 2023-07-26 02:01:30 +} +``` diff --git a/docs/datetime_zh-CN.md b/docs/datetime_zh-CN.md index 1669d0e..9c9d1df 100644 --- a/docs/datetime_zh-CN.md +++ b/docs/datetime_zh-CN.md @@ -59,6 +59,7 @@ import ( - [BetweenSeconds](#BetweenSeconds) - [DayOfYear](#DayOfYear) - [IsWeekend](#IsWeekend) +- [NowDateOrTime](#NowDateOrTime) @@ -1302,3 +1303,37 @@ func main() { // false } ``` + +### NowDateOrTime + +根据指定的格式和时区返回当前时间字符串。
+ +函数签名: + +```go +func NowDateOrTime(format string, timezone ...string) string +``` + +实例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/datetime" +) + +func main() { + result1 := datetime.NowDateOrTime("yyyy-mm-dd hh:mm:ss") + + result2 := datetime.NowDateOrTime("yyyy-mm-dd hh:mm:ss", "EST") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // 2023-07-26 15:01:30 + // 2023-07-26 02:01:30 +} +```