time

package
v1.32.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 14, 2026 License: MIT Imports: 2 Imported by: 0

README

Time Utilities

  • StartOfDay: Returns a time.Time set to the beginning (00:00:00) of the given day.

  • EndOfDay: Returns a time.Time set to the last moment (23:59:59.999999999) of the given day.

  • AddBusinessDays: Adds specified number of business days (excluding weekends) to a given date.

  • IsWeekend: Determines if a given date falls on a weekend (Saturday or Sunday).

  • TimeDifferenceHumanReadable: Converts time difference between two dates into a human-friendly string (e.g., "in 2 days" or "3 hours ago").

  • DurationUntilNext: Calculates duration until the next occurrence of a specific weekday.

  • ConvertToTimeZone: Converts a time.Time to a different timezone based on location name.

  • HumanReadableDuration: Formats a duration into a human-readable string (e.g., "2h 30m 45s").

  • CalculateAge: Computes age in years given a birth date.

  • IsLeapYear: Checks if a given year is a leap year.

  • NextOccurrence: Finds the next occurrence of a specific time on the same or next day.

  • WeekNumber: Returns the ISO year and week number for a given date.

  • DaysBetween: Calculates the number of days between two dates.

  • IsTimeBetween: Checks if a time falls between two other times.

  • UnixMilliToTime: Converts Unix milliseconds timestamp to time.Time.

  • SplitDuration: Breaks down a duration into days, hours, minutes, and seconds.

  • GetMonthName: Returns the English name of a month given its number (1-12).

  • GetDayName: Returns the English name of a day given its number (0-6).

  • FormatForDisplay: Formats a date in a readable format (e.g., "Monday, 2 Jan 2006").

  • IsToday: Checks if a given date is today.

Examples:

For examples of each function, please checkout EXAMPLES.md


Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddBusinessDays

func AddBusinessDays(t time.Time, days int) time.Time

AddBusinessDays adds the given number of business days to the given time

func CalculateAge

func CalculateAge(birthDate time.Time) int

CalculateAge calculates the age of a person given their birth date

func ConvertToTimeZone

func ConvertToTimeZone(t time.Time, location string) (time.Time, error)

ConvertToTimeZone converts the given time to the specified time zone

func DaysBetween

func DaysBetween(start, end time.Time) int

DaysBetween returns the number of days between two times

func DurationUntilNext

func DurationUntilNext(day time.Weekday, t time.Time) time.Duration

DurationUntilNext calculates the duration from the given time `t` to the next occurrence of the specified weekday `day`. If the target day is the same as the current day, it assumes the next occurrence of that day is in 7 days.

func EndOfDay

func EndOfDay(t time.Time) time.Time

EndOfDay returns the end of the day for the given time

func FormatForDisplay

func FormatForDisplay(t time.Time) string

func GetDayName

func GetDayName(dayNumber int) (string, error)

GetDayName returns the name of the day for the given day number

func GetMonthName

func GetMonthName(monthNumber int) (string, error)

GetMonthName returns the name of the month for the given month number

func HumanReadableDuration

func HumanReadableDuration(d time.Duration) string

HumanReadableDuration returns a human-readable string representing the given duration

func IsLeapYear

func IsLeapYear(year int) bool

IsLeapYear checks if the given year is a leap year

func IsTimeBetween

func IsTimeBetween(t, start, end time.Time) bool

IsTimeBetween checks if the given time is between the start and end times

func IsToday

func IsToday(t time.Time) bool

func IsWeekend

func IsWeekend(t time.Time) bool

IsWeekend checks if the given time is a weekend

func NextOccurrence

func NextOccurrence(hour, minute, second int, t time.Time) time.Time

NextOccurrence returns the next occurrence of the specified time on the same day as the given time

func SplitDuration

func SplitDuration(d time.Duration) (days, hours, minutes, seconds int)

SplitDuration splits the given duration into days, hours, minutes, and seconds

func StartOfDay

func StartOfDay(t time.Time) time.Time

StartOfDay returns the start of the day for the given time

func TimeDifferenceHumanReadable

func TimeDifferenceHumanReadable(from, to time.Time) string

TimeDifferenceHumanReadable returns a human-readable string representing the time difference between two times

func UnixMilliToTime

func UnixMilliToTime(ms int64) time.Time

UnixMilliToTime converts milliseconds since epoch to time.Time

func WeekNumber

func WeekNumber(t time.Time) (int, int)

WeekNumber returns the year and week number for the given time

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL