diff --git a/datetime/datetime.go b/datetime/datetime.go index 178a090..9009160 100644 --- a/datetime/datetime.go +++ b/datetime/datetime.go @@ -96,6 +96,18 @@ func GetNowDateTime() string { return time.Now().Format("2006-01-02 15:04:05") } +// GetTodayStartTime return the start time of today, format: yyyy-mm-dd 00:00:00. +// Play: todo +func GetTodayStartTime() string { + return time.Now().Format("2006-01-02") + " 00:00:00" +} + +// GetTodayEndTime return the end time of today, format: yyyy-mm-dd 23:59:59. +// Play: todo +func GetTodayEndTime() string { + return time.Now().Format("2006-01-02") + " 23:59:59" +} + // GetZeroHourTimestamp return timestamp of zero hour (timestamp of 00:00). // Play: https://go.dev/play/p/QmL2oIaGE3q func GetZeroHourTimestamp() int64 { diff --git a/datetime/datetime_test.go b/datetime/datetime_test.go index 3cbacbe..61232d0 100644 --- a/datetime/datetime_test.go +++ b/datetime/datetime_test.go @@ -91,6 +91,18 @@ func TestGetNowDateTime(t *testing.T) { assert.Equal(expected, GetNowDateTime()) } +func TestGetTodayStartTime(t *testing.T) { + assert := internal.NewAssert(t, "TestGetTodayStartTime") + expected := time.Now().Format("2006-01-02") + " 00:00:00" + assert.Equal(expected, GetTodayStartTime()) +} + +func TestGetTodayEndTime(t *testing.T) { + assert := internal.NewAssert(t, "TestGetTodayEndTime") + expected := time.Now().Format("2006-01-02") + " 23:59:59" + assert.Equal(expected, GetTodayEndTime()) +} + func TestFormatTimeToStr(t *testing.T) { assert := internal.NewAssert(t, "TestFormatTimeToStr") diff --git a/docs/datetime.md b/docs/datetime.md index 0087ed8..6d29666 100644 --- a/docs/datetime.md +++ b/docs/datetime.md @@ -42,6 +42,8 @@ import ( - [GetNowDate](#GetNowDate) - [GetNowTime](#GetNowTime) - [GetNowDateTime](#GetNowDateTime) +- [GetTodayStartTime](#GetTodayStartTime) +- [GetTodayEndTime](#GetTodayEndTime) - [GetZeroHourTimestamp](#GetZeroHourTimestamp) - [GetNightTimestamp](#GetNightTimestamp) - [FormatTimeToStr](#FormatTimeToStr) @@ -643,14 +645,11 @@ package main import ( "fmt" - "time" "github.com/duke-git/lancet/v2/datetime" ) func main() { - now := time.Now() currentDate := datetime.GetNowDate() - fmt.Println(currentDate) // Output: @@ -675,14 +674,11 @@ package main import ( "fmt" - "time" "github.com/duke-git/lancet/v2/datetime" ) func main() { - now := time.Now() currentTime := datetime.GetNowTime() - fmt.Println(currentTime) // 15:57:33 // Output: @@ -707,14 +703,11 @@ package main import ( "fmt" - "time" "github.com/duke-git/lancet/v2/datetime" ) func main() { - now := time.Now() current := datetime.GetNowDateTime() - fmt.Println(current) // Output: @@ -722,6 +715,64 @@ func main() { } ``` +### GetTodayStartTime + +

Return the start time of today, format: yyyy-mm-dd 00:00:00.

+ +Signature: + +```go +func GetTodayStartTime() string +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/datetime" +) + +func main() { + startTime := datetime.GetTodayStartTime() + fmt.Println(startTime) + + // Output: + // 2023-06-29 00:00:00 +} +``` + +### GetTodayEndTime + +

Return the end time of today, format: yyyy-mm-dd 23:59:59.

+ +Signature: + +```go +func GetTodayEndTime() string +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/datetime" +) + +func main() { + endTime := datetime.GetTodayEndTime() + fmt.Println(endTime) + + // Output: + // 2023-06-29 23:59:59 +} +``` + ### GetZeroHourTimestamp

Return timestamp of zero hour (timestamp of 00:00).

@@ -739,14 +790,11 @@ package main import ( "fmt" - "time" "github.com/duke-git/lancet/v2/datetime" ) func main() { - now := time.Now() zeroTime := datetime.GetZeroHourTimestamp() - fmt.Println(zeroTime) // Output: @@ -771,14 +819,11 @@ package main import ( "fmt" - "time" "github.com/duke-git/lancet/v2/datetime" ) func main() { - now := time.Now() nightTime := datetime.GetNightTimestamp() - fmt.Println(nightTime) // Output: diff --git a/docs/datetime_zh-CN.md b/docs/datetime_zh-CN.md index 8b9e901..9f1775e 100644 --- a/docs/datetime_zh-CN.md +++ b/docs/datetime_zh-CN.md @@ -41,6 +41,8 @@ import ( - [GetNowDate](#GetNowDate) - [GetNowTime](#GetNowTime) - [GetNowDateTime](#GetNowDateTime) +- [GetTodayStartTime](#GetTodayStartTime) +- [GetTodayEndTime](#GetTodayEndTime) - [GetZeroHourTimestamp](#GetZeroHourTimestamp) - [GetNightTimestamp](#GetNightTimestamp) - [FormatTimeToStr](#FormatTimeToStr) @@ -643,14 +645,11 @@ package main import ( "fmt" - "time" "github.com/duke-git/lancet/v2/datetime" ) func main() { - now := time.Now() currentDate := datetime.GetNowDate() - fmt.Println(currentDate) // Output: @@ -675,15 +674,12 @@ package main import ( "fmt" - "time" "github.com/duke-git/lancet/v2/datetime" ) func main() { - now := time.Now() currentTime := datetime.GetNowTime() - - fmt.Println(currentTime) // 15:57:33 + fmt.Println(currentTime) // Output: // 15:57:33 @@ -707,14 +703,11 @@ package main import ( "fmt" - "time" "github.com/duke-git/lancet/v2/datetime" ) func main() { - now := time.Now() current := datetime.GetNowDateTime() - fmt.Println(current) // Output: @@ -722,6 +715,64 @@ func main() { } ``` +### GetTodayStartTime + +

返回当天开始时间, 格式: yyyy-mm-dd 00:00:00.

+ +函数签名: + +```go +func GetTodayStartTime() string +``` + +示例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/datetime" +) + +func main() { + startTime := datetime.GetTodayStartTime() + fmt.Println(startTime) + + // Output: + // 2023-06-29 00:00:00 +} +``` + +### GetTodayEndTime + +

返回当天结束时间,格式: yyyy-mm-dd 23:59:59.

+ +函数签名: + +```go +func GetTodayEndTime() string +``` + +示例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/datetime" +) + +func main() { + endTime := datetime.GetTodayEndTime() + fmt.Println(endTime) + + // Output: + // 2023-06-29 23:59:59 +} +``` + ### GetZeroHourTimestamp

获取零点时间戳(timestamp of 00:00)

@@ -739,14 +790,11 @@ package main import ( "fmt" - "time" "github.com/duke-git/lancet/v2/datetime" ) func main() { - now := time.Now() zeroTime := datetime.GetZeroHourTimestamp() - fmt.Println(zeroTime) // Output: @@ -771,14 +819,11 @@ package main import ( "fmt" - "time" "github.com/duke-git/lancet/v2/datetime" ) func main() { - now := time.Now() nightTime := datetime.GetNightTimestamp() - fmt.Println(nightTime) // Output: