date

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2026 License: MIT Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package date provides calendar helpers, human-readable relative time formatting, an application-scoped clock, and a calendar Date type without a time of day.

Use ForHumans, DurationForHumans, IsPast, IsWeekend, StartOfDay, EndOfDay, and DiffInDays to inspect and describe time.Time values:

date.ForHumans(ctx, post.CreatedAt)          // "3 minutes ago" or "in 2 hours"
date.DurationForHumans(incident.Duration())  // "4 minutes"
date.IsPast(ctx, trialEndsAt)                // true once the trial is over
date.IsWeekend(order.PlacedAt)               // true on a Saturday or Sunday
date.StartOfDay(t), date.EndOfDay(t)         // the day's first and last instants
date.DiffInDays(user.CreatedAt, trialEndsAt) // 30

Represent a calendar date without a time of day, such as a due date or birthday, with Date. Call Now, Today, or TodayIn to read the current time from the application clock carried in ctx.

Functions that compare a time against the current instant take a context.Context and read the application's clock from it. Calendar operations evaluate dates in the time's own time.Location.

For custom string formatting, call time.Time.Format with a Go layout such as "January 2, 2006". For duration arithmetic, call time.Time.Add or time.Time.AddDate, or use AddMonths and AddYears to clamp overflowing days to the end of shorter months.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddMonths

func AddMonths(t time.Time, months int) time.Time

AddMonths shifts t forward (or backward for negative values) by the given number of calendar months while preserving its time of day and location. If the target month has fewer days than t's day of month, the day is clamped to the end of that month. For example, adding 1 month to January 31 returns February 28 (or February 29 in a leap year), whereas time.Time.AddDate overflows into March:

renewsAt := date.AddMonths(subscription.StartedAt, 1)

func AddYears

func AddYears(t time.Time, years int) time.Time

AddYears shifts t forward (or backward for negative values) by the given number of calendar years, clamping overflowing days like AddMonths. For example, adding 1 year to February 29 returns February 28.

func CompactForHumans

func CompactForHumans(ctx context.Context, t time.Time) string

CompactForHumans formats the relative time like ForHumans using abbreviated unit suffixes such as "4m ago" or "in 2h". Values under one second still return "just now". CompactForHumans panics if ctx does not carry an application scope.

func DiffInDays

func DiffInDays(a, b time.Time) int

DiffInDays returns the number of whole calendar days from a to b, returning a negative count when b is earlier than a. Days are measured in a's location so a day ends at the same wall-clock time on the following date, even across a 23-hour or 25-hour daylight saving transition. For an exact elapsed duration, use b.Sub(a) instead.

func DiffInMonths

func DiffInMonths(a, b time.Time) int

DiffInMonths returns the number of whole calendar months from a to b, returning a negative count when b is earlier than a. A month completes on the same day and time of the following month, so January 31 to February 28 counts as 0 whole months, and January 15 to March 14 counts as 1.

func DurationForHumans

func DurationForHumans(d time.Duration) string

DurationForHumans formats a time.Duration in its largest whole unit, such as "less than a second", "45 seconds", "4 minutes", "1 hour", or "2 weeks". Negative durations are formatted using their absolute length.

func EndOfDay

func EndOfDay(t time.Time) time.Time

EndOfDay returns the last nanosecond (23:59:59.999999999) of t's date in t's location. Together, StartOfDay and EndOfDay form an inclusive range for a full calendar day, such as when querying a day's orders.

func FirstOfMonth

func FirstOfMonth(t time.Time) time.Time

FirstOfMonth returns midnight on the first day of t's month in t's location.

func ForHumans

func ForHumans(ctx context.Context, t time.Time) string

ForHumans describes how far in the past or future t is relative to Now, expressed in the largest whole unit, such as "just now", "3 minutes ago", "in 2 hours", or "1 year ago".

Days, weeks, months, and years follow the calendar in t's location, matching DiffInDays and DiffInMonths. For example, 30 days from October 5 returns "in 4 weeks", and February 1 to March 1 counts as 1 month. ForHumans panics if ctx does not carry an application scope.

func IsBetween

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

IsBetween reports whether t is between start and end, inclusive of both endpoints.

func IsFuture

func IsFuture(ctx context.Context, t time.Time) bool

IsFuture reports whether t is later than Now. It panics if ctx does not carry an application scope.

func IsPast

func IsPast(ctx context.Context, t time.Time) bool

IsPast reports whether t is earlier than Now. It panics if ctx does not carry an application scope.

func IsSameDay

func IsSameDay(a, b time.Time) bool

IsSameDay reports whether a and b fall on the same calendar date in a's location.

func IsToday

func IsToday(ctx context.Context, t time.Time) bool

IsToday reports whether t falls on today's calendar date in t's location. To check against a user's local day, convert the timestamp first with t.In(location). For calendar dates without a time of day, such as a due date, compare a Date against TodayIn instead. IsToday panics if ctx does not carry an application scope.

func IsTomorrow

func IsTomorrow(ctx context.Context, t time.Time) bool

IsTomorrow reports whether t falls on tomorrow's calendar date in t's location. It panics if ctx does not carry an application scope.

func IsWeekday

func IsWeekday(t time.Time) bool

IsWeekday reports whether t falls on Monday through Friday in t's location.

func IsWeekend

func IsWeekend(t time.Time) bool

IsWeekend reports whether t falls on a Saturday or Sunday in t's location.

func IsYesterday

func IsYesterday(ctx context.Context, t time.Time) bool

IsYesterday reports whether t falls on yesterday's calendar date in t's location. It panics if ctx does not carry an application scope.

func LastOfMonth

func LastOfMonth(t time.Time) time.Time

LastOfMonth returns the last nanosecond of the final day of t's month in t's location. Together, FirstOfMonth and LastOfMonth bound a full calendar month.

func Now

func Now(ctx context.Context) time.Time

Now returns the current time from the application clock carried in ctx. In production this matches time.Now, while tests can freeze or advance it using laraveltest.Travel or laraveltest.Freeze.

Laravel reads the clock through Now for sessions, cache expiration, signed URLs, rate limits, delayed jobs, and model timestamps. Reading time through Now in your own code lets tests control time without sleeping:

if post.PublishAt.After(date.Now(r)) {
	return web.Abort(http.StatusNotFound)
}

Now panics if ctx does not carry an application scope.

func StartOfDay

func StartOfDay(t time.Time) time.Time

StartOfDay returns midnight (00:00:00) at the beginning of t's date in t's location.

func StartOfWeek

func StartOfWeek(t time.Time) time.Time

StartOfWeek returns midnight on the Monday that begins t's week in t's location.

Types

type Date

type Date struct {
	Year  int
	Month time.Month
	Day   int
}

Date represents a calendar day without a time of day or time zone, such as an invoice due date or a user's birthday:

type Invoice struct {
	ID    int64     `json:"id"`
	DueOn date.Date `json:"due_on"`
}

Date scans from and writes to SQL DATE columns (use *Date for nullable columns), and encodes to JSON and text in YYYY-MM-DD format so form inputs and route parameters decode into it automatically. Compare dates with ==, Date.Before, and Date.After.

func Of

func Of(t time.Time) Date

Of extracts the calendar Date of t in t's location. For a date in another time zone, convert t first with Of(t.In(loc)).

func Parse

func Parse(s string) (Date, error)

Parse parses a calendar date formatted as YYYY-MM-DD (such as "2026-10-05") and returns an error if s does not match that format.

func Today

func Today(ctx context.Context) Date

Today returns the current calendar Date in the location of Now, which is typically the server's time zone or UTC. When you need the current date in a specific user's time zone, use TodayIn instead, because shortly after midnight UTC it may still be yesterday in another time zone. Today panics if ctx does not carry an application scope.

func TodayIn

func TodayIn(ctx context.Context, loc *time.Location) Date

TodayIn returns the current calendar Date in loc, such as a user's local time zone:

today := date.TodayIn(r, chicago)
if invoice.DueOn.Before(today) {
	// The invoice is overdue...
}

TodayIn panics if ctx does not carry an application scope.

func (Date) AddDays

func (d Date) AddDays(n int) Date

AddDays returns the Date that is n days after d, or earlier when n is negative.

func (Date) AddMonths

func (d Date) AddMonths(months int) Date

AddMonths returns the Date that is months calendar months after d (or earlier when negative), clamping overflowing days like AddMonths. For example, adding 1 month to January 31 returns February 28.

func (Date) AddYears

func (d Date) AddYears(years int) Date

AddYears returns the Date that is years calendar years after d (or earlier when negative), clamping February 29 to February 28 in non-leap years.

func (Date) After

func (d Date) After(other Date) bool

After reports whether d is chronologically later than other.

func (Date) Before

func (d Date) Before(other Date) bool

Before reports whether d is chronologically earlier than other.

func (Date) CompactForHumans

func (d Date) CompactForHumans(ctx context.Context) string

CompactForHumans formats the relative date like Date.ForHumans using abbreviated unit suffixes such as "in 3d" or "2w ago", keeping "today" unchanged. CompactForHumans panics if ctx does not carry an application scope.

func (Date) DaysUntil

func (d Date) DaysUntil(other Date) int

DaysUntil returns the number of calendar days from d to other, returning a negative count when other is earlier than d.

func (Date) ForHumans

func (d Date) ForHumans(ctx context.Context) string

ForHumans describes how far in the past or future d is relative to Today, expressed in the largest whole unit, such as "today", "in 3 days", "2 weeks ago", or "in 1 month". To compare against a user's local day, use TodayIn and Date.DaysUntil instead. ForHumans panics if ctx does not carry an application scope.

func (Date) Format

func (d Date) Format(layout string) string

Format formats d using a time.Time.Format layout such as "January 2, 2006". Any time-of-day components in the layout format as midnight UTC.

func (Date) In

func (d Date) In(loc *time.Location) time.Time

In returns midnight (00:00:00) on date d in the given time.Location.

func (Date) IsZero

func (d Date) IsZero() bool

IsZero reports whether d is the zero value Date{}.

func (Date) MarshalText

func (d Date) MarshalText() ([]byte, error)

MarshalText encodes d as UTF-8 text in YYYY-MM-DD format.

func (*Date) Scan

func (d *Date) Scan(src any) error

Scan implements database/sql.Scanner, reading a time.Time, string, or byte slice from a SQL DATE column. Scan returns an error for NULL values or unsupported types; scan nullable columns into a *Date instead.

func (Date) String

func (d Date) String() string

String formats d in ISO 8601 date format (YYYY-MM-DD), such as "2026-10-05".

func (*Date) UnmarshalText

func (d *Date) UnmarshalText(text []byte) error

UnmarshalText parses a YYYY-MM-DD date string into d and returns an error if text is not a valid date.

func (Date) Value

func (d Date) Value() (driver.Value, error)

Value implements driver.Valuer by returning d formatted as YYYY-MM-DD.

func (Date) Weekday

func (d Date) Weekday() time.Weekday

Weekday returns the day of the week on which d falls.

Jump to

Keyboard shortcuts

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