# Datetime Package datetime supports date and time format and compare.
## Source: [https://github.com/duke-git/lancet/blob/main/datetime/datetime.go](https://github.com/duke-git/lancet/blob/main/datetime/datetime.go)
## Usage: ```go import ( "github.com/duke-git/lancet/datetime" ) ```
## Index - [AddDay](#AddDay) - [AddHour](#AddHour) - [AddMinute](#AddMinute) - [GetNowDate](#GetNowDate) - [GetNowTime](#GetNowTime) - [GetNowDateTime](#GetNowDateTime) - [GetZeroHourTimestamp](#GetZeroHourTimestamp) - [GetNightTimestamp](#GetNightTimestamp) - [FormatTimeToStr](#FormatTimeToStr) - [FormatStrToTime](#FormatStrToTime)
## Documentation ## Note: 1. 'format' string param in func FormatTimeToStr and FormatStrToTime function should be one of flows: - yyyy-mm-dd hh:mm:ss - yyyy-mm-dd hh:mm - yyyy-mm-dd hh - yyyy-mm-dd - yyyy-mm - mm-dd - dd-mm-yy hh:mm:ss - yyyy/mm/dd hh:mm:ss - yyyy/mm/dd hh:mm - yyyy-mm-dd hh - yyyy/mm/dd - yyyy/mm - mm/dd - dd/mm/yy hh:mm:ss - yyyy - mm - hh:mm:ss - mm:ss ### AddDay

Add or sub days to time.

Signature: ```go func AddDay(t time.Time, day int64) time.Time ``` Example: ```go package main import ( "fmt" "time" "github.com/duke-git/lancet/datetime" ) func main() { now := time.Now() after2Days := datetime.AddDay(now, 2) before2Days := datetime.AddDay(now, -2) fmt.Println(after2Days, before2Days) } ``` ### AddHour

Add or sub hours to time.

Signature: ```go func AddHour(t time.Time, hour int64) time.Time ``` Example: ```go package main import ( "fmt" "time" "github.com/duke-git/lancet/datetime" ) func main() { now := time.Now() after2Hours := datetime.AddHour(now, 2) before2Hours := datetime.AddHour(now, -2) fmt.Println(after2Hours, after2Hours) } ``` ### AddMinute

Add or sub minutes to time.

Signature: ```go func AddMinute(t time.Time, minute int64) time.Time ``` Example: ```go package main import ( "fmt" "time" "github.com/duke-git/lancet/datetime" ) func main() { now := time.Now() after2Minute := datetime.AddMinute(now, 2) before2Minute := datetime.AddMinute(now, -2) fmt.Println(after2Minute, before2Minute) } ``` ### GetNowDate

Get current date string, format is yyyy-mm-dd.

Signature: ```go func GetNowDate() string ``` Example: ```go package main import ( "fmt" "time" "github.com/duke-git/lancet/datetime" ) func main() { now := time.Now() currentDate := datetime.GetNowDate() fmt.Println(currentDate) // 2022-01-28 } ``` ### GetNowTime

Get current time string, format is hh:mm:ss.

Signature: ```go func GetNowTime() string ``` Example: ```go package main import ( "fmt" "time" "github.com/duke-git/lancet/datetime" ) func main() { now := time.Now() currentTime := datetime.GetNowTime() fmt.Println(currentDate) // 15:57:33 } ``` ### GetNowDateTime

Get current date time string, format is yyyy-mm-dd hh:mm:ss.

Signature: ```go func GetNowDateTime() string ``` Example: ```go package main import ( "fmt" "time" "github.com/duke-git/lancet/datetime" ) func main() { now := time.Now() current := datetime.GetNowDateTime() fmt.Println(current) // 2022-01-28 15:59:33 } ``` ### GetZeroHourTimestamp

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

Signature: ```go func GetZeroHourTimestamp() int64 ``` Example: ```go package main import ( "fmt" "time" "github.com/duke-git/lancet/datetime" ) func main() { now := time.Now() zeroTime := datetime.GetZeroHourTimestamp() fmt.Println(zeroTime) // 1643299200 } ``` ### GetNightTimestamp

Return timestamp of zero hour (timestamp of 23:59).

Signature: ```go func GetNightTimestamp() int64 ``` Example: ```go package main import ( "fmt" "time" "github.com/duke-git/lancet/datetime" ) func main() { now := time.Now() nightTime := datetime.GetNightTimestamp() fmt.Println(nightTime) // 1643385599 } ``` ### FormatTimeToStr

Format time to string, `format` param specification see note 1.

Signature: ```go func FormatTimeToStr(t time.Time, format string) string ``` Example: ```go package main import ( "fmt" "time" "github.com/duke-git/lancet/datetime" ) func main() { now := time.Now() timeStr := datetime.FormatTimeToStr(now, "yyyy/mm/dd hh:mm:ss") fmt.Println(timeStr) //2022/01/28 16:07:44 } ``` ### FormatStrToTime

Format string to time, `format` param specification see note 1.

Signature: ```go func FormatStrToTime(str, format string) (time.Time, error) ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/datetime" ) func main() { time := datetime.FormatStrToTime("2006-01-02 15:04:05", "yyyy/mm/dd hh:mm:ss") fmt.Println(time) } ```