- Date type in Golang
- Unit testing
- NullDate support
- Some methods to working with Date type
const RFC3339Date = "2006-01-02"RFC3339Date format
func RemoveTime(t time.Time) time.TimeRemoveTime removes the time values of time.Time at UTC Ex: 2020-01-01T15:30:30Z07:00 -> 2020-01-01T00:00:00Z
type Date time.TimeDate is the non-nullable type for Date only. Support UTC timezone only
func MustParse(s string) DateMustParse creates a new Date from the RFC3339 Date - "2006-01-02" Panic if wrong format
func New(t time.Time) DateNew creates a new Date
func NewDate(year int, month time.Month, day int) DateNewDate from year, month and day
func NewFromStr(s string) (Date, error)NewFromStr creates a new Date from the RFC3339 Date - "2006-01-02"
func NewZero() DateNewZero creates the new zero (null) Date
func (d Date) AddDate(year, month, day int) DateAddDate returns the Date corresponding to adding the given number of years, months, and days to d. For example, AddDate(-1, 2, 3) applied to January 1, 2011 returns March 4, 2010.
AddDate normalizes its result in the same way that Date does, so, for example, adding one month to October 31 yields December 1, the normalized form for November 31.
func (d Date) AddMonths(n int) DateAddMonths return add the number of months. If the day at this month > maxDay (like 30 for Feb), it will be the last day of this month
func (d Date) After(ref Date) boolAfter compares date if it's after or not
func (d Date) Before(ref Date) boolBefore compares date if it's before or not
func (d Date) Day() intDay returns the day of the NullDate
func (d Date) DaysOfMonth() intDaysOfMonth returns total days in the month of the Date
func (d Date) DiffMonths(ref Date) intDiffMonths to calculate the diff of months If date.After(ref) -> Return negative If date.Before(ref) -> Return positive Id date.Equal(ref) -> Return 1 a.DiffMonths(b) may != b.DiffMonths(a) because of the last day of month
func (d Date) Equal(ref Date) boolEqual to compare with another
func (d Date) GormDataType() stringGormDataType gorm common data type
func (d Date) IsZero() boolIsZero check if it is the zero value
func (d Date) MarshalJSON() ([]byte, error)MarshalJSON marshal to the JSON
func (d Date) Month() time.MonthMonth returns the month of the Date
func (d *Date) Scan(value interface{}) (err error)Scan implements the Scanner interface
func (d Date) String() stringStrong converts the Date to string as RFC3339 format
func (d Date) Sub(ref Date) time.DurationSub return duration date - ref
func (d Date) ToTime() time.TimeToTime converts the Date type to time.Time type
func (d *Date) UnmarshalJSON(bs []byte) errorUnmarshalJSON to parse the JSON
func (d Date) Value() (driver.Value, error)Value implements the driver Valuer interface
func (d Date) YMD() (year int, month time.Month, day int)YMD returns year, month and day of the Date
func (d Date) Year() intYear returns the year of the Date