Documentation ¶
Overview ¶
Package datetime provides a way to working with datetime.
Example Usage ¶
The following is a complete example using datetime package
import ( "time" "github.com/phamtai97/go-utils/utils/datetime" "github.com/phamtai97/go-utils/utils/logger" "go.uber.org/zap" ) func main() { logger.InitProduction("") // Convert current milliseconds to different formats logger.Info("Convert current milliseconds to format YYYY-MM-DD", zap.String("value", datetime.ConvertCurrentLocalTimeToString(datetime.YYYY_MM_DD))) logger.Info("Convert current milliseconds to format YYYY-MM-DD HH:mm:ss", zap.String("value", datetime.ConvertCurrentLocalTimeToString(datetime.YYYY_MM_DD_HH_MM_SS))) logger.Info("Convert current milliseconds to format YYYY-MM-DD HH:mm:ss.SSS", zap.String("value", datetime.ConvertCurrentLocalTimeToString(datetime.YYYY_MM_DD_HH_MM_SS_SSS))) logger.Info("Convert current milliseconds to format DD-MM-YYYY", zap.String("value", datetime.ConvertCurrentLocalTimeToString(datetime.DD_MM_YYYY))) logger.Info("Convert current milliseconds to format DD-MM-YYYY HH:mm:ss", zap.String("value", datetime.ConvertCurrentLocalTimeToString(datetime.DD_MM_YYYY_HH_MM_SS))) logger.Info("Convert current milliseconds to format DD-MM-YYYY HH:mm:ss.SSS", zap.String("value", datetime.ConvertCurrentLocalTimeToString(datetime.DD_MM_YYYY_HH_MM_SS_SSS))) // Get current millisenconds millis := datetime.GetCurrentMiliseconds() logger.Info("Current milliseconds", zap.Int64("value", millis)) // Convert milliseconds to different formats yyyymmdd := datetime.ConvertMillisecondsToString(millis, datetime.YYYY_MM_DD) logger.Info("Convert milliseconds to format YYYY-MM-DD", zap.String("value", yyyymmdd)) yyyymmdd_hhmmss := datetime.ConvertMillisecondsToString(millis, datetime.YYYY_MM_DD_HH_MM_SS) logger.Info("Convert milliseconds to format YYYY-MM-DD HH:mm:ss", zap.String("value", yyyymmdd_hhmmss)) yyyymmdd_hhmmss_sss := datetime.ConvertMillisecondsToString(millis, datetime.YYYY_MM_DD_HH_MM_SS_SSS) logger.Info("Convert milliseconds to format YYYY-MM-DD HH:mm:ss.SSS", zap.String("value", yyyymmdd_hhmmss_sss)) ddmmyyyy := datetime.ConvertMillisecondsToString(millis, datetime.DD_MM_YYYY) logger.Info("Convert milliseconds to format DD-MM-YYYY", zap.String("value", ddmmyyyy)) ddmmyyyy_hhmmss := datetime.ConvertMillisecondsToString(millis, datetime.DD_MM_YYYY_HH_MM_SS) logger.Info("Convert milliseconds to format DD-MM-YYYY HH:mm:ss", zap.String("value", ddmmyyyy_hhmmss)) ddmmyyyy_hhmmss_sss := datetime.ConvertMillisecondsToString(millis, datetime.DD_MM_YYYY_HH_MM_SS_SSS) logger.Info("Convert milliseconds to format DD-MM-YYYY HH:mm:ss.SSS", zap.String("value", ddmmyyyy_hhmmss_sss)) // Convert string format to millisecond millis, _ = datetime.ConvertStringToMilliseconds(yyyymmdd, datetime.YYYY_MM_DD) logger.Info("Convert string format YYYY-MM-DD to millisecond", zap.Int64("value", millis)) millis, _ = datetime.ConvertStringToMilliseconds(yyyymmdd_hhmmss, datetime.YYYY_MM_DD_HH_MM_SS) logger.Info("Convert string format YYYY-MM-DD HH:mm:ss to millisecond", zap.Int64("value", millis)) millis, _ = datetime.ConvertStringToMilliseconds(yyyymmdd_hhmmss_sss, datetime.YYYY_MM_DD_HH_MM_SS_SSS) logger.Info("Convert string format YYYY-MM-DD HH:mm:ss.SSS to millisecond", zap.Int64("value", millis)) millis, _ = datetime.ConvertStringToMilliseconds(ddmmyyyy, datetime.DD_MM_YYYY) logger.Info("Convert string format DD-MM-YYYY to millisecond", zap.Int64("value", millis)) millis, _ = datetime.ConvertStringToMilliseconds(ddmmyyyy_hhmmss, datetime.DD_MM_YYYY_HH_MM_SS) logger.Info("Convert string format DD-MM-YYYY HH:mm:ss to millisecond", zap.Int64("value", millis)) millis, _ = datetime.ConvertStringToMilliseconds(ddmmyyyy_hhmmss_sss, datetime.DD_MM_YYYY_HH_MM_SS_SSS) logger.Info("Convert string format DD-MM-YYYY HH:mm:ss.SSS to millisecond", zap.Int64("value", millis)) // Other functions logger.Info("Year", zap.Int("value", datetime.GetYear())) logger.Info("Day of year", zap.Int("value", datetime.GetDayOfYear())) logger.Info("Day of month", zap.Int("value", datetime.GetDayOfMonth())) logger.Info("Month of year", zap.Int("value", datetime.GetMonthOfYear())) logger.Info("Start local time of year", zap.Int64("value", datetime.ConvertLocalTimeToMilliseconds(datetime.GetStartLocalTimeOfYear()))) logger.Info("End local time of year", zap.Int64("value", datetime.ConvertLocalTimeToMilliseconds(datetime.GetEndLocalTimeOfYear()))) logger.Info("Start local time of month", zap.Int64("value", datetime.ConvertLocalTimeToMilliseconds(datetime.GetStartLocalTimeOfMonth()))) logger.Info("End local time of month", zap.Int64("value", datetime.ConvertLocalTimeToMilliseconds(datetime.GetEndLocalTimeOfMonth()))) logger.Info("Start local time of day", zap.Int64("value", datetime.ConvertLocalTimeToMilliseconds(datetime.GetStartLocalTimeOfDay()))) logger.Info("End local time of day", zap.Int64("value", datetime.ConvertLocalTimeToMilliseconds(datetime.GetEndLocalTimeOfDay()))) logger.Info("Start local time of time", zap.Int64("value", datetime.ConvertLocalTimeToMilliseconds(datetime.GetStartLocalTimeOfTime(time.Now())))) logger.Info("Start local time of time", zap.Int64("value", datetime.ConvertLocalTimeToMilliseconds(datetime.GetEndLocalTimeOfTime(time.Now())))) logger.Info("Get before local time of time", zap.Int64("value", datetime.ConvertLocalTimeToMilliseconds(datetime.GetBeforeLocalTimeOfTime(time.Now(), 9, true)))) logger.Info("Get after local time of time", zap.Int64("value", datetime.ConvertLocalTimeToMilliseconds(datetime.GetAfterLocalTimeOfTime(time.Now(), 9, false)))) }
Index ¶
- Constants
- func ConvertCurrentLocalTimeToString(format string) string
- func ConvertLocalTimeToMilliseconds(t time.Time) int64
- func ConvertLocalTimeToString(localTime time.Time, format string) string
- func ConvertMillisecondsToLocalTime(millis int64) time.Time
- func ConvertMillisecondsToString(millis int64, format string) string
- func ConvertStringToLocalTime(value, format string) (time.Time, error)
- func ConvertStringToMilliseconds(value, format string) (int64, error)
- func GetAfterLocalTimeOfTime(inputTime time.Time, numberDay int, isStartTime bool) time.Time
- func GetBeforeLocalTimeOfTime(inputTime time.Time, numberDay int, isStartTime bool) time.Time
- func GetCurrentLocalTime() time.Time
- func GetCurrentMiliseconds() int64
- func GetDayOfMonth() int
- func GetDayOfYear() int
- func GetEndLocalTimeOfDay() time.Time
- func GetEndLocalTimeOfMonth() time.Time
- func GetEndLocalTimeOfTime(inputTime time.Time) time.Time
- func GetEndLocalTimeOfYear() time.Time
- func GetMillisecondsBetween(startDate time.Time, endTime time.Time) int64
- func GetMonthOfYear() int
- func GetStartLocalTimeOfDay() time.Time
- func GetStartLocalTimeOfMonth() time.Time
- func GetStartLocalTimeOfTime(inputTime time.Time) time.Time
- func GetStartLocalTimeOfYear() time.Time
- func GetYear() int
Constants ¶
const ( // YYYY_MM_DD_HH_MM_SS_SSS is the format "2006-01-02 15:04:05.000". YYYY_MM_DD_HH_MM_SS_SSS = "2006-01-02 15:04:05.000" // YYYY_MM_DD_HH_MM_SS is the format "2006-01-02 15:04:05". YYYY_MM_DD_HH_MM_SS = "2006-01-02 15:04:05" // YYYY_MM_DD is the format "2006-01-02". YYYY_MM_DD = "2006-01-02" // DD_MM_YYYY is the format "02-01-2006". DD_MM_YYYY = "02-01-2006" // DD_MM_YYYY_HH_MM_SS is the format "02-01-2006 15:04:05". DD_MM_YYYY_HH_MM_SS = "02-01-2006 15:04:05" // DD_MM_YYYY_HH_MM_SS_SSS is the format "02-01-2006 15:04:05.000". DD_MM_YYYY_HH_MM_SS_SSS = "02-01-2006 15:04:05.000" )
Variables ¶
This section is empty.
Functions ¶
func ConvertCurrentLocalTimeToString ¶
ConvertCurrentLocalTimeToString converts the current local time to string with the specific format.
func ConvertLocalTimeToMilliseconds ¶
ConvertLocalTimeToMilliseconds converts the local time to milliseconds.
func ConvertLocalTimeToString ¶
ConvertLocalTimeToString converts the local time to string with specific format.
func ConvertMillisecondsToLocalTime ¶
ConvertMillisecondsToLocalTime converts the milliseconds to local time.
func ConvertMillisecondsToString ¶
ConvertMillisecondsToString converts the milliseconds to the string with the specific format.
func ConvertStringToLocalTime ¶
ConvertStringToLocalTime converts the string with specific format to the local time.
func ConvertStringToMilliseconds ¶
ConvertStringToMilliseconds converts the string with specific format to milliseconds.
func GetAfterLocalTimeOfTime ¶
GetAfterLocalTimeOfTime returns after local time with the number of days compared to specific local time.
func GetBeforeLocalTimeOfTime ¶
GetBeforeLocalTimeOfTime returns before local time with the number of days compared to specific local time.
func GetCurrentLocalTime ¶
GetCurrentLocalTime returns the current local time.
func GetCurrentMiliseconds ¶
func GetCurrentMiliseconds() int64
GetCurrentMiliseconds returns the current milliseconds.
func GetEndLocalTimeOfDay ¶
GetEndLocalTimeOfDay return the end local time of day.
func GetEndLocalTimeOfMonth ¶
GetEndLocalTimeOfMonth returns the end local time of month.
func GetEndLocalTimeOfTime ¶
GetEndLocalTimeOfTime return the end local time of specific local time.
func GetEndLocalTimeOfYear ¶
GetEndLocalTimeOfYear returns the end local time of year.
func GetMillisecondsBetween ¶
GetMillisecondsBetween returns the number of milliseconds between 2 local time.
func GetMonthOfYear ¶
func GetMonthOfYear() int
GetMonthOfYear returns the month of year. Value from 1 to 12.
func GetStartLocalTimeOfDay ¶
GetStartLocalTimeOfDay return the start local time of day.
func GetStartLocalTimeOfMonth ¶
GetStartLocalTimeOfMonth returns the start local time of month.
func GetStartLocalTimeOfTime ¶
GetStartLocalTimeOfTime return the start local time of specific local time.
func GetStartLocalTimeOfYear ¶
GetStartLocalTimeOfYear returns the start local time of year.
Types ¶
This section is empty.