Documentation
¶
Index ¶
- func Diff(d1, d2 Date) (year int, month int, day int)
- type Date
- func FromString(str string) (Date, error)
- func FromTime(t time.Time) Date
- func Max(d1, d2 Date) Date
- func Min(d1, d2 Date) Date
- func MustFromString(str string) Date
- func New(year int, month time.Month, day int) Date
- func Today() Date
- func TodayIn(loc *time.Location) Date
- func Tomorrow() Date
- func TomorrowIn(loc *time.Location) Date
- func Yesterday() Date
- func YesterdayIn(loc *time.Location) Date
- func (d Date) AddCalendarMonths(months int) Date
- func (d Date) AddDays(days int) Date
- func (d Date) AddMonths(months int) Date
- func (d Date) AddYears(years int) Date
- func (d Date) Day() int
- func (d Date) DaysInMonth() int
- func (d Date) DaysInYear() int
- func (d Date) EndOfMonth() Date
- func (d Date) MarshalJSON() ([]byte, error)
- func (d Date) Month() time.Month
- func (d *Date) Scan(src any) error
- func (d Date) StartOfMonth() Date
- func (d Date) StartOfNextQuarter() Date
- func (d Date) StartOfQuarter() Date
- func (d Date) String() string
- func (d Date) Time() time.Time
- func (d Date) TimeIn(loc *time.Location) time.Time
- func (d *Date) UnmarshalJSON(p []byte) error
- func (d Date) Value() (driver.Value, error)
- func (d Date) Weekday() time.Weekday
- func (d Date) Year() int
- func (d Date) YearDay() int
- type NullDate
- type NullDateJsonField
- type NullDateSqlField
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Diff ¶ added in v1.1.0
Diff finds the difference in years, months, and days between two provided dates. The dates can be provided in any order, or can be equal.
Returns 3 integers representing the number of years, months, and days separating the two dates. These values are non-contiguous and should be taken as standalone values, e.g., the diff between Feb 1st 2001 and March 1st 2002 would be 1 year, 13 months, and 393 days.
Types ¶
type Date ¶
type Date int
Date represents a calendar day (and thus has day precision). It's stored as the number of days since the epoch date 1970-01-01. It can be negative, to represent dates before the epoch. Because it's a date, it doesn't exist within any particular timezone.
func FromString ¶
FromString creates a date from its ISO8601 (YYYY-MM-DD) representation.
func Max ¶
Max finds the maximum date (furthest in the direction of the future) out of the two given dates.
func Min ¶
Min finds the minimum date (furthest in the direction of the past) out of the two given dates.
func MustFromString ¶
MustFromString creates a date from its ISO8601 (YYYY-MM-DD) representation. It panics if str is not in the right format.
func TomorrowIn ¶
Tomorrow gives tomorrow's date in given timezone.
func YesterdayIn ¶
Yesterday gives yesterday's date in given timezone.
func (Date) AddCalendarMonths ¶ added in v1.2.0
AddCalendarMonths adds the number of specified months to create a new date. This function does not allow rollover and dates are clamped to the end of the month. e.g. adding 1 month to 2025-03-31 will result in 2025-04-30 rather than 2025-05-01 with the AddMonths function.
func (Date) AddDays ¶
AddDays adds the number of specified days to create a new date. Since Dates are just integers representing the number of days since 1970-01-01, the usual `+` operator can be used instead.
func (Date) AddMonths ¶
AddMonths adds the number of specified months to create a new date. Dates are normalized in the same way as `AddDate` in the `time` package.
func (Date) AddYears ¶
AddYears adds the number of specified years to create a new date. Dates are normalized in the same way as `AddDate` in the `time` package.
func (Date) DaysInMonth ¶
DaysInMonth gives the number of days in the current month
func (Date) DaysInYear ¶
DaysInYear gives how many total days the year has (365/366)
func (Date) EndOfMonth ¶
EndOfMonth gives the date that is the last day of the current month.
func (Date) MarshalJSON ¶
MarshalJSON marshals the date into a JSON string in ISO8601 format.
func (*Date) Scan ¶
Scan implements the sql.Scanner interface, allowing the sql package to read sql dates into Date.
func (Date) StartOfMonth ¶
StartOfMonth gives the date that is the 1st day of the current month.
func (Date) StartOfNextQuarter ¶
StartOfNextQuarter gives the date that is the 1st day of the next quarter (starting in Jan, Apr, Jul, or Oct).
func (Date) StartOfQuarter ¶
StartOfQuarter gives the date that is the 1st day of the current quarter (starting in Jan, Apr, Jul, or Oct).
func (Date) Time ¶
Time returns a time.Time at midnight at the start of the date in the UTC timezone.
func (Date) TimeIn ¶
TimeIn returns a time.Time at midnight at the start of the date in the Location specified.
func (*Date) UnmarshalJSON ¶
UnmarshalJSON unmarshals a JSON string in the ISO8601 format into a date.
func (Date) Value ¶
Value implements the driver.Valuer interface, allowing sql drivers to send Dates to sql databases.
type NullDate ¶ added in v1.1.0
NullDate represents a nullable date with compatibility for scanning null values from the database.
func NewNullDate ¶ added in v1.1.0
func (NullDate) MarshalJSON ¶ added in v1.1.0
MarshalJSON implements the json.Marshaler interface.
func (NullDate) Scan ¶ added in v1.1.0
Scan implements the sql.Scanner interface for database deserialization.
func (NullDate) UnmarshalJSON ¶ added in v1.1.0
UnmarshalJSON implements the json.Unmarshaler interface.
type NullDateJsonField ¶ added in v1.1.0
type NullDateJsonField interface { json.Unmarshaler json.Marshaler }
NullDate should properly implement UnmarshalJSON and MarshalJSON