dateHelpers

package
v1.22.0 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

README

dateHelpers

dateHelpers provides comprehensive date/time formatting, parsing, arithmetic, comparison, and business-day adjustment utilities.

Types

Type Description
DateFormat Struct holding all Go time layout strings (External, DMY, Internal, Detail, YMD, Calendar, BackupDate, BackupFolder, HTMLInput, DMY4, MYH)

Variables

Variable Description
Format The active DateFormat instance, populated from configuration

Functions

Formatting and Parsing

Paired format/parse functions for each date layout:

Format Parse
FormatAudit(time.Time) string ParseAudit(string) (time.Time, error)
FormatDMY(time.Time) string ParseDMY(string) (time.Time, error)
FormatYMD(time.Time) string ParseYMD(string) (time.Time, error)
FormatCalendar(time.Time) string ParseCalendar(string) (time.Time, error)
FormatHuman(time.Time) string ParseHuman(string) (time.Time, error)
FormatInternal(time.Time) string ParseInternal(string) (time.Time, error)
FormatHTMLInput(time.Time) string ParseHTMLInput(string) (time.Time, error)
FormatDMY4(time.Time) string ParseDMY4(string) (time.Time, error)
FormatDetail(time.Time) string ParseDetail(string) (time.Time, error)
FormatExternal(time.Time) string ParseExternal(string) (time.Time, error)
FormatBackupDate(time.Time) string ParseBackupDate(string) (time.Time, error)
FormatBackupFolder(time.Time) string ParseBackupFolder(string) (time.Time, error)
FormatHumanFromString(string) string ParseHumanFromString(string) (time.Time, error)
Day Info
Function Description
Today() time.Time Returns today's date
Tomorrow() time.Time Returns tomorrow's date
Yesterday() time.Time Returns yesterday's date
StartOfDay(time.Time) time.Time Returns midnight of the given day
EndOfDay(time.Time) time.Time Returns 23:59:59 of the given day
IsWorkingDay(time.Time) bool Returns true if Monday–Friday
NextWorkingDay(time.Time) time.Time Returns the next business day
PreviousWorkingDay(time.Time) time.Time Returns the previous business day
AdjustToNextWorkingday(time.Time) time.Time Adjusts to next working day if on a weekend
AdjustToPreviousWorkingday(time.Time) time.Time Adjusts to previous working day if on a weekend
DifferenceInDays(a, b time.Time) int Returns the number of days between two dates
OrdinaliseDay(int) string Returns ordinal suffix (e.g. "1st", "2nd")
HumanDate(time.Time) string Returns a friendly date string
Comparisons
Function Description
IsSameDay(a, b time.Time) bool True if same calendar day
IsTomorrow(time.Time) bool True if the date is tomorrow
IsYesterday(time.Time) bool True if the date is yesterday
IsToday(time.Time) bool True if the date is today
IsBefore / IsAfter(a, b time.Time) bool Date ordering
IsBeforeOrEqualTo / IsAfterOrEqualTo(a, b) bool Inclusive date ordering
IsBetweenDates(d, start, end time.Time) bool True if within range
IsWorkingDateBetween(d, start, end) bool True if within range and a working day
IsWeekend / IsWeekday(time.Time) bool Day-of-week checks
IsMonday … IsSunday(time.Time) bool Individual day checks
IsAfterDays / IsBeforeDays(d, n) bool True if date is after/before N days from now
IsInXDays / IsNotInXDays(d, n) bool True if exactly/not exactly N days from now
Arithmetic
Function Description
AddDays / SubtractDays(t, n) time.Time Add/subtract calendar days
AddWorkingDays / SubtractWorkingDays(t, n) time.Time Add/subtract business days
AddWeeks / SubtractWeeks(t, n) time.Time Add/subtract weeks
AddWorkingWeeks / SubtractWorkingWeeks(t, n) time.Time Add/subtract working weeks
AddMonths / SubtractMonths(t, n) time.Time Add/subtract months
AddWorkingMonths / SubtractWorkingMonths(t, n) time.Time Add/subtract working months
AddYears / SubtractYears(t, n) time.Time Add/subtract years
AddWorkingYears / SubtractWorkingYears(t, n) time.Time Add/subtract working years
AddMonthAndAdjust(t, n) time.Time Add months with end-of-month adjustment

Example

import "github.com/mt1976/frantic-core/dateHelpers"

func main() {
    today := dateHelpers.Today()
    tomorrow := dateHelpers.Tomorrow()
    fmt.Println("Today:", dateHelpers.FormatHuman(today))
    fmt.Println("Tomorrow:", dateHelpers.FormatHuman(tomorrow))
    fmt.Println("Is working day:", dateHelpers.IsWorkingDay(today))

    nextMonth := dateHelpers.AddWorkingMonths(today, 1)
    fmt.Println("Next working month:", dateHelpers.FormatHuman(nextMonth))
}

Documentation

Overview

Package dateHelpers provides utilities for date/time formatting, parsing, arithmetic, and business-day adjustments used across the project.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddDays added in v1.3.4

func AddDays(startDate time.Time, daysToAdd int) time.Time

AddDays returns the time that is daysToAdd days after startDate.

func AddMonthAndAdjust added in v1.2.72

func AddMonthAndAdjust(startDate time.Time, noMonths int) time.Time

AddMonthAndAdjust adds noMonths calendar months and adjusts to the next weekday if needed.

func AddMonths added in v1.3.4

func AddMonths(startDate time.Time, monthsToAdd int) time.Time

AddMonths returns the time that is monthsToAdd months after startDate.

func AddWeeks added in v1.3.4

func AddWeeks(startDate time.Time, weeksToAdd int) time.Time

func AddWorkingDays added in v1.2.72

func AddWorkingDays(startDate time.Time, daysToAdd int) time.Time

AddWorkingDays adds n working days (excluding weekends) to a given date.

func AddWorkingMonths added in v1.3.4

func AddWorkingMonths(startDate time.Time, monthsToAdd int) time.Time

func AddWorkingWeeks added in v1.3.4

func AddWorkingWeeks(startDate time.Time, weeksToAdd int) time.Time

func AddWorkingYears added in v1.3.4

func AddWorkingYears(startDate time.Time, yearsToAdd int) time.Time

func AddYears added in v1.3.4

func AddYears(startDate time.Time, yearsToAdd int) time.Time

func AdjustToNextWorkingday added in v1.2.72

func AdjustToNextWorkingday(date time.Time) time.Time

AdjustToNextWorkingday moves the date forward to the next weekday if it's a weekend.

func AdjustToPreviousWorkingday added in v1.3.4

func AdjustToPreviousWorkingday(date time.Time) time.Time

AdjustToPreviousWorkingday moves the date backward to the previous weekday if it's a weekend.

func DifferenceInDays added in v1.3.4

func DifferenceInDays(t1, t2 time.Time) int

func EndOfDay

func EndOfDay(t time.Time) time.Time

EndOfDay returns the final second of the given date (23:59:59).

func FormatAudit

func FormatAudit(in time.Time) string

FormatAudit returns the formatted date/time using the audit/detail layout.

func FormatBackupDate added in v1.3.4

func FormatBackupDate(in time.Time) string

FormatBackupDate formats a time for backup naming (date component).

func FormatBackupFolder added in v1.3.4

func FormatBackupFolder(in time.Time) string

FormatBackupFolder formats a time for backup folder naming.

func FormatCalendar

func FormatCalendar(in time.Time) string

FormatCalendar formats the time using the calendar (ISO-like) layout.

func FormatDMY

func FormatDMY(in time.Time) string

FormatDMY formats a time as day/month/year using the configured DMY layout.

func FormatDMY4 added in v1.3.4

func FormatDMY4(in time.Time) string

FormatDMY4 formats a time as dd/mm/yyyy using the configured DMY4 layout.

func FormatDetail added in v1.3.4

func FormatDetail(in time.Time) string

FormatDetail returns a detailed date/time string including time components.

func FormatExternal added in v1.3.4

func FormatExternal(in time.Time) string

FormatExternal returns a human/external formatted date/time string.

func FormatHTMLInput added in v1.3.4

func FormatHTMLInput(in time.Time) string

FormatHTMLInput formats a time for HTML input controls (e.g. dd/mm/yyyy).

func FormatHuman

func FormatHuman(t time.Time) string

FormatHuman returns the human-friendly formatted date/time string using the External layout.

func FormatHumanFromString

func FormatHumanFromString(in string) (time.Time, error)

FormatHumanFromString parses a human-readable date/time string using the External layout. This is functionally equivalent to ParseHumanFromString.

func FormatInternal added in v1.3.4

func FormatInternal(in time.Time) string

FormatInternal returns the internal/system formatted date/time string.

func FormatYMD

func FormatYMD(in time.Time) string

FormatYMD formats a time as year-month-day using the configured YMD layout.

func HumanDate added in v1.5.0

func HumanDate(t time.Time) string

func IsAfter added in v1.3.4

func IsAfter(t1, t2 time.Time) bool

func IsAfterDays added in v1.3.4

func IsAfterDays(t1, t2 time.Time, days int) bool

func IsAfterOrEqualTo

func IsAfterOrEqualTo(t1, t2 time.Time) bool

IsAfterOrEqualTo reports whether t1 is strictly after or equal to t2 when compared at the start of day.

func IsBefore added in v1.3.4

func IsBefore(t1, t2 time.Time) bool

func IsBeforeDays added in v1.3.4

func IsBeforeDays(t1, t2 time.Time, days int) bool

func IsBeforeOrEqualTo

func IsBeforeOrEqualTo(t1, t2 time.Time) bool

IsBeforeOrEqualTo reports whether t1 is strictly before or equal to t2 when both are compared at their respective start-of-day values.

func IsBetweenDates added in v1.3.4

func IsBetweenDates(testDate, startDate, endDate time.Time) bool

func IsFriday added in v1.3.4

func IsFriday(t time.Time) bool

func IsInXDays added in v1.3.4

func IsInXDays(t1, t2 time.Time, days int) bool

func IsMonday added in v1.3.4

func IsMonday(t time.Time) bool

func IsNotInXDays added in v1.3.4

func IsNotInXDays(t1, t2 time.Time, days int) bool

func IsSameDay added in v1.3.4

func IsSameDay(t1, t2 time.Time) bool

IsSameDay reports whether two times fall on the same calendar day.

func IsSaturday added in v1.3.4

func IsSaturday(t time.Time) bool

func IsSunday added in v1.3.4

func IsSunday(t time.Time) bool

func IsThursday added in v1.3.4

func IsThursday(t time.Time) bool

func IsToday added in v1.3.4

func IsToday(t time.Time) bool

IsToday reports whether the given time is today (relative to now).

func IsTomorrow added in v1.3.4

func IsTomorrow(t time.Time) bool

IsTomorrow reports whether the given time is tomorrow (relative to now).

func IsTuesday added in v1.3.4

func IsTuesday(t time.Time) bool

func IsWednesday added in v1.3.4

func IsWednesday(t time.Time) bool

func IsWeekday added in v1.3.4

func IsWeekday(t time.Time) bool

func IsWeekend added in v1.3.4

func IsWeekend(t time.Time) bool

func IsWorkingDateBetween added in v1.3.4

func IsWorkingDateBetween(testDate, startDate, endDate time.Time) bool

func IsWorkingDay added in v1.2.72

func IsWorkingDay(t time.Time) bool

IsWorkingDay reports whether the date falls on a weekday (Mon-Fri).

func IsYesterday added in v1.3.4

func IsYesterday(t time.Time) bool

IsYesterday reports whether the given time is yesterday (relative to now).

func NextWorkingDay added in v1.3.4

func NextWorkingDay() time.Time

NextWorkingDay returns the next working day after today.

func OrdinaliseDay added in v1.5.0

func OrdinaliseDay(x int) string

func ParseAudit added in v1.3.4

func ParseAudit(in string) (time.Time, error)

ParseAudit parses a date/time string using the audit/detail layout.

func ParseBackupDate added in v1.3.4

func ParseBackupDate(in string) (time.Time, error)

ParseBackupDate parses a backup date string.

func ParseBackupFolder added in v1.3.4

func ParseBackupFolder(in string) (time.Time, error)

ParseBackupFolder parses a backup folder date/time string.

func ParseCalendar added in v1.3.4

func ParseCalendar(in string) (time.Time, error)

ParseCalendar parses a date/time string using the calendar layout.

func ParseDMY added in v1.3.4

func ParseDMY(in string) (time.Time, error)

ParseDMY parses a day/month/year string using the configured DMY layout.

func ParseDMY4 added in v1.3.4

func ParseDMY4(in string) (time.Time, error)

ParseDMY4 parses a dd/mm/yyyy string using the configured DMY4 layout.

func ParseDetail added in v1.3.4

func ParseDetail(in string) (time.Time, error)

ParseDetail parses a detailed date/time string using the Detail layout.

func ParseExternal added in v1.3.4

func ParseExternal(in string) (time.Time, error)

ParseExternal parses a human/external date/time string.

func ParseHTMLInput added in v1.3.4

func ParseHTMLInput(in string) (time.Time, error)

ParseHTMLInput parses a string formatted for HTML input controls.

func ParseHuman added in v1.3.4

func ParseHuman(in string) (time.Time, error)

ParseHuman parses a human-friendly date/time string using the External layout.

func ParseHumanFromString added in v1.3.4

func ParseHumanFromString(in string) (time.Time, error)

ParseHumanFromString parses a human-readable date/time string using the External layout.

func ParseInternal added in v1.3.4

func ParseInternal(in string) (time.Time, error)

ParseInternal parses a date/time string using the internal/system layout.

func ParseYMD added in v1.3.4

func ParseYMD(in string) (time.Time, error)

ParseYMD parses a year-month-day string using the configured YMD layout.

func PreviousWorkingDay added in v1.3.4

func PreviousWorkingDay() time.Time

PreviousWorkingDay returns the previous working day before today.

func StartOfDay

func StartOfDay(t time.Time) time.Time

StartOfDay returns the time truncated to midnight in the local timezone.

func SubtractDays added in v1.3.4

func SubtractDays(startDate time.Time, daysToSubtract int) time.Time

func SubtractMonths added in v1.3.4

func SubtractMonths(startDate time.Time, monthsToSubtract int) time.Time

func SubtractWeeks added in v1.3.4

func SubtractWeeks(startDate time.Time, weeksToSubtract int) time.Time

func SubtractWorkingDays added in v1.3.4

func SubtractWorkingDays(startDate time.Time, daysToSubtract int) time.Time

func SubtractWorkingMonths added in v1.3.4

func SubtractWorkingMonths(startDate time.Time, monthsToSubtract int) time.Time

func SubtractWorkingWeeks added in v1.3.4

func SubtractWorkingWeeks(startDate time.Time, weeksToSubtract int) time.Time

func SubtractWorkingYears added in v1.3.4

func SubtractWorkingYears(startDate time.Time, yearsToSubtract int) time.Time

SubtractWorkingYears subtracts full years and adjusts to the previous working day if needed.

func SubtractYears added in v1.3.4

func SubtractYears(startDate time.Time, yearsToSubtract int) time.Time

SubtractYears returns the time that is yearsToSubtract years before startDate.

func Today added in v1.3.4

func Today() time.Time

Today returns the current date truncated to the start of day.

func Tomorrow added in v1.3.4

func Tomorrow() time.Time

Tomorrow returns the time representing 24 hours from now.

func Yesterday added in v1.3.4

func Yesterday() time.Time

Yesterday returns the time representing 24 hours before now.

Types

type DateFormat

type DateFormat struct {
	External     string
	DMY          string
	Internal     string
	Detail       string
	YMD          string
	Calendar     string
	BackupDate   string
	BackupFolder string
	HTMLInput    string
	DMY4         string
	MYH          string
}

DateFormat defines the collection of Go time layouts used by this package for formatting/parsing dates in various contexts.

var Format DateFormat

Format contains the active set of date/time layout strings loaded from application configuration. These layouts are used by the helper functions for formatting and parsing dates in consistent forms.

Jump to

Keyboard shortcuts

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