Documentation
¶
Index ¶
- func Convert(value, fromLayout, toLayout string) (string, error)
- func DateValue(date time.Time) int
- func Days(days int, dt ...time.Time) time.Time
- func DaysInMonth(year, month int) int
- func DaysInQuarter(year, quarter int) int
- func DaysInYear(year int) int
- func Diff(t1, t2 time.Time, unit time.Duration, rounded ...bool) float64
- func Earliest(t1, t2 time.Time, tn ...time.Time) time.Time
- func EoD(t ...time.Time) time.Time
- func Format(dt time.Time, layout string) string
- func FormatTimestamp(timestamp int64, layout string) string
- func FormatUnix(sec int64, nsec int64, layout string) string
- func IsBetween(t1, t2, t3 time.Time) bool
- func IsBetweenDates(t1, t2, t3 time.Time) bool
- func IsLeapYear(year int) bool
- func LastMonth() time.Time
- func LastWeek() time.Time
- func LastYear() time.Time
- func Latest(t1, t2 time.Time, tn ...time.Time) time.Time
- func MonthEnd(dt ...time.Time) time.Time
- func MonthStart(dt ...time.Time) time.Time
- func Months(months int, dt ...time.Time) time.Time
- func NetWorkDays(startDate, endDate time.Time, workingDays [7]bool, holidays ...time.Time) int
- func NewDate(year, month, day int) time.Time
- func NewTime(hour, minute, second int) time.Time
- func NextMonth() time.Time
- func NextWeek() time.Time
- func NextYear() time.Time
- func Parse(layout, value string) (time.Time, error)
- func ParseInLocation(layout, value string, loc *time.Location) (time.Time, error)
- func PrevWorkDay(startDate time.Time, days int, workingDays [7]bool, holidays ...time.Time) time.Time
- func ReplaceDate(t time.Time, year, month, day int) time.Time
- func ReplaceTime(t time.Time, hour, minute, second int) time.Time
- func SoD(t ...time.Time) time.Time
- func TimeAgo(t time.Time, baseTime ...time.Time) string
- func Tomorrow() time.Time
- func TruncateTime(date time.Time) time.Time
- func WeekEnd(dt ...time.Time) time.Time
- func WeekEndOn(day time.Weekday, dt ...time.Time) time.Time
- func WeekStart(dt ...time.Time) time.Time
- func WeekStartOn(day time.Weekday, dt ...time.Time) time.Time
- func Weeks(weeks int, dt ...time.Time) time.Time
- func WorkDay(startDate time.Time, days int, workingDays [7]bool, holidays ...time.Time) time.Time
- func YearEnd(dt ...time.Time) time.Time
- func YearStart(dt ...time.Time) time.Time
- func Years(years int, dt ...time.Time) time.Time
- func Yesterday() time.Time
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Convert ¶
ParseAndFormat is a utility function that takes a date-time string, a source format string (from), and a target format string (to). The function parses the input date-time string according to the source format, and then formats the parsed date-time value using the target format. The function returns the formatted date-time string and an error if the parsing or formatting fails.
Example usage:
formattedDate, err := ParseAndFormat("2022-12-31", "yyyy-mm-dd", "dt mmmm, yyyy")
if err != nil {
panic(err)
}
fmt.Println(formattedDate) // 31st December, 2022
func DateValue ¶
DateValue returns the serial number of the given time.Time
Arguments ¶
date: (time.Time) The date to be converted to serial number
Note ¶
The serial number is the number of days from 1/1/1900
func Days ¶
Days returns the date of the given number of days from the date provided,
Arguments ¶
days: (int) The number of days to be added to the date.
dt: (time.Time) The date to be used to calculate the date of the given number of days. (Only takes the first date if multiple dates are provided)
Note ¶
If the days parameter is 0 it will panic.
func DaysInMonth ¶
DaysInMonth returns the number of days in the month.
For February, the number of days depends on whether the year is a leap year or not. For April, June, September, and November, the number of days is 30. For all other months, the number of days is 31.
Example:
days := DaysInMonth(2022, 2) // days == 28
func DaysInQuarter ¶
DaysInQuarter returns the number of days in the quarter.
The number of days in each quarter depends on the number of days in each month. Q1: Jan (31) + Feb (28 or 29) + Mar (31) = 90 or 91 days Q2: Apr (30) + May (31) + Jun (30) = 91 days Q3: Jul (31) + Aug (31) + Sep (30) = 92 days Q4: Oct (31) + Nov (30) + Dec (31) = 92 days
Example:
days := DaysInQuarter(2022, 1) // days == 90
func DaysInYear ¶
DaysInYear returns the number of days in the year.
If the year is a leap year, it returns 366, otherwise it returns 365.
Example:
days := DaysInYear(2022) // days == 365
func Diff ¶
Diff returns the difference between the given time.Time and the current time.Time in the given unit
func Earliest ¶
Earliest returns the earliest time from the given time.Time list
Arguments ¶
t1: (time.Time) The first time to be compared
t2: (time.Time) The second time to be compared
tn: (time.Time) The rest of the times to be compared
func EoD ¶
EoD returns the end of the day for the given time.
It calculates the start of the day for the given time using the SoD function, and adds 24 hours to it. To get the end of the day, it subtracts one nanosecond from the resulting time.Time value, since the SoD function returns the first nanosecond of the day.
Example:
t := time.Date(2022, time.December, 30, 16, 30, 0, 0, time.UTC) endOfDay := EoD(t) // endOfDay == time.Date(2022, time.December, 30, 23, 59, 59, 999999999, time.UTC)
func Format ¶
Format is a utility function that takes a time.Time value and a layout string as input, then converts the time value to a formatted string based on the layout.
Example usage:
formattedDate := Format(time.Now(), "yyyy-mm-dd")
func FormatTimestamp ¶
FormatTimestamp takes a Unix timestamp in seconds and a layout string, then returns a formatted string based on the layout.
Example usage:
formattedDate := FormatTimestamp(1609459200, "yyyy-mm-dd")
func FormatUnix ¶
FormatUnix takes the Unix time in seconds and nanoseconds, as well as a layout string, and returns a formatted string based on the layout.
Example usage:
formattedDate := FormatUnix(1609459200, 0, "yyyy-mm-dd")
func IsBetween ¶
IsBetween returns true if the given time.Time is between the given time.Time range
Arguments ¶
t1: (time.Time) The date to be checked
t2: (time.Time) The first date of the range
t3: (time.Time) The second date of the range
func IsBetweenDates ¶
IsBetweenDates checks if the given time is in the range of the start and end date. Before performing inclusive comparison, it sets the time to the start of the day for the start date and the end of the day for the end date.
func IsLeapYear ¶
IsLeapYear returns true if the year is a leap year.
A leap year is a year that is evenly divisible by 4, but not by 100 unless it is also divisible by 400.
Example:
isLeap := IsLeapYear(2024) // isLeap == true
func Latest ¶
Latest returns the latest time from the given time.Time list
Arguments ¶
t1: (time.Time) The first time to be compared
t2: (time.Time) The second time to be compared
tn: (time.Time) The rest of the times to be compared
func MonthEnd ¶
MonthEnd returns the last day and the last second of the month.
Arguments ¶
dt: (time.Time) The date to be used to calculate the last day of the month.
Note ¶
If the date is not provided, it will return the last day of the month from the current date.
func MonthStart ¶
MonthStart returns the first day of the month.
Arguments ¶
dt: (time.Time) The date to be used to calculate the first day of the month.
Note ¶
If the date is not provided, it will return the first day of the month from the current date.
func Months ¶
Months returns the date of the given number of months from the date provided,
Arguments ¶
months: (int) The number of months to be added to the date.
dt: (time.Time) The date to be used to calculate the date of the given number of months. (Only takes the first date if multiple dates are provided)
Note ¶
If the months parameter is 0 it will panic.
func NetWorkDays ¶
NetWorkdays returns the number of working days between the given dates
Arguments ¶
startDate: (time.Time) The date to start from
endDate: (time.Time) The date to end at
workingDays: ([7]bool) The working days of the week (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday)
holidays: (...time.Time) The holidays to be excluded
func NewDate ¶
NewDate creates a time.Time object from the given year, month and day.
It constructs a new time.Time object with the given year, month, and day, and sets the hour, minute, second, and nanosecond fields to 0. It also sets the location field to time.UTC.
Example:
date := NewDate(2022, 4, 15) // date == time.Date(2022, time.April, 15, 0, 0, 0, 0, time.UTC)
func NewTime ¶
NewTime creates a time.Time object from the given hour, minute and second.
It constructs a new time.Time object with the given hour, minute, and second, and sets the year, month, and day fields to 0. It also sets the location field to time.UTC.
Example:
timeObj := NewTime(12, 30, 0) // timeObj == time.Date(0, 1, 1, 12, 30, 0, 0, time.UTC)
func Parse ¶
Parse is a utility function that takes a date-time string and a format string as input, then parses the date-time string according to the format. The function returns a time.Time value and an error if the parsing fails.
Example usage:
parsedDate, err := Parse("yyyy-mm-dd", "2022-12-31")
if err != nil {
panic(err)
}
fmt.Println(parsedDate) // 2022-12-31 00:00:00 +0000 UTC
func ParseInLocation ¶
ParseInLocation is a utility function that takes a date-time string, a format string as input, then parses the date-time string according to the format. The function returns a time.Time value and an error if the parsing fails. It also takes a location as input and returns the time in that location.
Example usage:
parsedDate, err := ParseInLocation("yyyy-mm-dd", "2022-12-31", time.FixedZone("IST", 5.5*60*60))
if err != nil {
panic(err)
}
fmt.Println(parsedDate) // 2022-12-31 00:00:00 +0000 UTC
func PrevWorkDay ¶
func PrevWorkDay(startDate time.Time, days int, workingDays [7]bool, holidays ...time.Time) time.Time
PrevWorkDay returns the date before the given number of working days
Arguments ¶
startDate: (time.Time) The date to start from
days: (int) The number of working days to subtract
workingDays: ([7]bool) The working days of the week (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday)
holidays: (...time.Time) The holidays to be excluded
Note ¶
The working days are the days that are not holidays and are in the working days of the week
func ReplaceDate ¶
ReplaceDate lets you replace the date part of a time.Time object with a new date. It keeps the time part of the time.Time object unchanged.
Example:
date := time.Date(2022, time.April, 15, 12, 30, 0, 0, time.UTC) newDate := ReplaceDate(date, 2023, 5, 20) // newDate == time.Date(2023, time.May, 20, 12, 30, 0, 0, time.UTC)
func ReplaceTime ¶
ReplaceTime lets you replace the time part of a time.Time object with a new time. It keeps the date part of the time.Time object unchanged.
Example:
date := time.Date(2022, time.April, 15, 12, 30, 0, 0, time.UTC) newTime := ReplaceTime(date, 15, 45, 0) // newTime == time.Date(2022, time.April, 15, 15, 45, 0, 0, time.UTC)
func SoD ¶
SoD returns the start of the day for the given time.
It constructs a new time.Time value using the year, month, and day of the given time.Time value, and setting the hour, minute, second, and nanosecond fields to 0. It also sets the location field to the same location as the input time.Time value. The resulting time.Time value represents the start of the day for the given time.
Example:
t := time.Date(2022, time.December, 30, 16, 30, 0, 0, time.UTC) startOfDay := SoD(t) // startOfDay == time.Date(2022, time.December, 30, 0, 0, 0, 0, time.UTC)
func TimeAgo ¶
TimeAgo calculates the relative time difference between a given timestamp and the current time, and returns a string that describes the time difference in human-readable terms. The function takes a time.Time object representing the timestamp, and an optional baseTime parameter, which can be used to specify a different base time to use instead of the current time. The function returns a string that describes the time difference in human-readable terms, such as "2 weeks ago" or "In a few minutes".
Example usage:
// Create a time object representing one week ago
oneWeekAgo := time.Now().Add(-7 * 24 * time.Hour)
// Calculate the relative time difference between the timestamp and the current time
timeAgo := TimeAgo(oneWeekAgo)
fmt.Println("One week ago:", timeAgo)
// Output: One week ago: Last week
// Create a time object representing 3 hours and 45 minutes ago
threeHoursAgo := time.Now().Add(-3 * time.Hour).Add(-45 * time.Minute)
// Calculate the relative time difference between the timestamp and the current time
timeAgo = TimeAgo(threeHoursAgo)
fmt.Println("Three hours ago:", timeAgo)
// Output: Three hours ago: 3 hours ago
func TruncateTime ¶
TruncateTime truncates the time part of the given date. It returns the tructed date.
Arguments ¶
date: (time.Time) The date to be truncated
func WeekEnd ¶
WeekEnd returns the last day and the last second of the week.
Arguments ¶
dt: (time.Time) The date to be used to calculate the last day of the week.
Note ¶
If the date is not provided, it will return the last day of the week from the current date.
func WeekEndOn ¶
WeekEndOn returns the last day and the last second of the week on the given day. For example, WeekEndOn(time.Sunday) returns the last day of the week (Sunday).
Arguments ¶
day: (time.Weekday) The day to be used to calculate the last day of the week.
dt: (time.Time) The date to be used to calculate the last day of the week.
Note ¶
If the date is not provided, it will return the last day of the week from the current date.
func WeekStart ¶
WeekStart returns the first day of the week (Monday).
Arguments ¶
dt: (time.Time) The date to be used to calculate the first day of the week.
Note ¶
If the date is not provided, it will return the first day of the week from the current date.
func WeekStartOn ¶
WeekStartOn returns the first day of the week on the given day.
Arguments ¶
day: (time.Weekday) The day to be used to calculate the first day of the week.
dt: (time.Time) The date to be used to calculate the first day of the week.
Note ¶
If the date is not provided, it will return the first day of the week from the current date.
func Weeks ¶
Weeks returns the date of the given number of weeks from the current date. If the value is negative, it will return the date of the previous week.
Arguments ¶
weeks: (int) The number of weeks to be added to the date.
dt: (time.Time) The date to be used to calculate the date of the given number of weeks. (Only takes the first date if multiple dates are provided)
Note ¶
If the weeks parameter is 0 it will panic.
func WorkDay ¶
WorkDay returns the date after the given number of working days
Arguments ¶
startDate: (time.Time) The date to start from
days: (int) The number of working days to add
workingDays: ([7]bool) The working days of the week (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday)
holidays: (...time.Time) The holidays to be excluded
Note ¶
The working days are the days that are not holidays and are in the working days of the week
func YearEnd ¶
YearEnd returns the last day and the last second of the year.
Arguments ¶
dt: (time.Time) The date to be used to calculate the last day of the year.
Note ¶
If the date is not provided, it will return the last day of the year from the current date.
func YearStart ¶
-----------------Year Functions----------------- YearStart returns the first day of the year.
Arguments ¶
dt: (time.Time) The date to be used to calculate the first day of the year.
Note ¶
If the date is not provided, it will return the first day of the year from the current date.
func Years ¶
Years returns the date of the given number of years from the date provided, If the date is not provided, it will return the date of the given number of years from the current date.
Arguments ¶
years: (int) The number of years to be added to the date.
dt: (time.Time) The date to be used to calculate the date of the given number of year. (Only takes the first date if multiple dates are provided)
Note ¶
If the years parameter is 0 it will panic.
Types ¶
This section is empty.