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 ¶
- func AddMonths(t time.Time, months int) time.Time
- func AddYears(t time.Time, years int) time.Time
- func CompactForHumans(ctx context.Context, t time.Time) string
- func DiffInDays(a, b time.Time) int
- func DiffInMonths(a, b time.Time) int
- func DurationForHumans(d time.Duration) string
- func EndOfDay(t time.Time) time.Time
- func FirstOfMonth(t time.Time) time.Time
- func ForHumans(ctx context.Context, t time.Time) string
- func IsBetween(t, start, end time.Time) bool
- func IsFuture(ctx context.Context, t time.Time) bool
- func IsPast(ctx context.Context, t time.Time) bool
- func IsSameDay(a, b time.Time) bool
- func IsToday(ctx context.Context, t time.Time) bool
- func IsTomorrow(ctx context.Context, t time.Time) bool
- func IsWeekday(t time.Time) bool
- func IsWeekend(t time.Time) bool
- func IsYesterday(ctx context.Context, t time.Time) bool
- func LastOfMonth(t time.Time) time.Time
- func Now(ctx context.Context) time.Time
- func StartOfDay(t time.Time) time.Time
- func StartOfWeek(t time.Time) time.Time
- type Date
- func (d Date) AddDays(n int) Date
- func (d Date) AddMonths(months int) Date
- func (d Date) AddYears(years int) Date
- func (d Date) After(other Date) bool
- func (d Date) Before(other Date) bool
- func (d Date) CompactForHumans(ctx context.Context) string
- func (d Date) DaysUntil(other Date) int
- func (d Date) ForHumans(ctx context.Context) string
- func (d Date) Format(layout string) string
- func (d Date) In(loc *time.Location) time.Time
- func (d Date) IsZero() bool
- func (d Date) MarshalText() ([]byte, error)
- func (d *Date) Scan(src any) error
- func (d Date) String() string
- func (d *Date) UnmarshalText(text []byte) error
- func (d Date) Value() (driver.Value, error)
- func (d Date) Weekday() time.Weekday
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddMonths ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
FirstOfMonth returns midnight on the first day of t's month in t's location.
func ForHumans ¶
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 IsFuture ¶
IsFuture reports whether t is later than Now. It panics if ctx does not carry an application scope.
func IsPast ¶
IsPast reports whether t is earlier than Now. It panics if ctx does not carry an application scope.
func IsToday ¶
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 ¶
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 IsYesterday ¶
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 ¶
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 ¶
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 ¶
StartOfDay returns midnight (00:00:00) at the beginning of t's date in t's location.
Types ¶
type Date ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
AddDays returns the Date that is n days after d, or earlier when n is negative.
func (Date) AddMonths ¶
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 ¶
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) CompactForHumans ¶
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 ¶
DaysUntil returns the number of calendar days from d to other, returning a negative count when other is earlier than d.
func (Date) ForHumans ¶
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 ¶
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 ¶
In returns midnight (00:00:00) on date d in the given time.Location.
func (Date) MarshalText ¶
MarshalText encodes d as UTF-8 text in YYYY-MM-DD format.
func (*Date) Scan ¶
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) UnmarshalText ¶
UnmarshalText parses a YYYY-MM-DD date string into d and returns an error if text is not a valid date.
func (Date) Value ¶
Value implements driver.Valuer by returning d formatted as YYYY-MM-DD.