Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Diff ¶
type Diff struct { Years int Months int Weeks int Days int // contains filtered or unexported fields }
Diff describes dates difference in years, months, weeks, and days.
func NewDiff ¶
NewDiff creates Diff according to the provided format. Provided format should contain special "verbs" that define dates difference calculattion logic. These are supported format verbs:
%Y - to calculate dates difference in years %M - to calculate dates difference in months %W - to calculate dates difference in weeks %D - to calculate dates difference in days
When format contains multiple "verbs" the date diffrence will be calculated starting from longest time unit to shortest. For example:
start, _ := time.Parse("2006-01-02", "2000-04-17") end, _ := time.Parse("2006-01-02", "2003-03-16") diff1, _ := NewDiff(start, end, "%Y") diff2, _ := NewDiff(start, end, "%M") diff3, _ := NewDiff(start, end, "%Y %M") fmt.Println(diff1) // 2 years fmt.Println(diff2) // 34 month fmt.Println(diff3) // 2 years 10 months
NewDiff returns error in the following cases:
start date is after end date format contains unsupported "verb" undefined dates difference mode (it happens when the format does not contain any of the supported "verbs")
func NewDiffWithMode ¶ added in v0.2.0
NewDiffWithMode creates Diff according to the provided mode. There are four modes defined:
ModeYears ModeMonths ModeWeeks ModeDays
Modes can be combined to support multiple date units. For example:
start, _ := time.Parse("2006-01-02", "2000-04-17") end, _ := time.Parse("2006-01-02", "2003-03-16") diff1, _ := NewDiffWithMode(start, end, ModeYears) diff2, _ := NewDiffWithMode(start, end, ModeMonths) diff3, _ := NewDiffWithMode(start, end, ModeYears | ModeMonths) fmt.Println(diff1) // 2 years fmt.Println(diff2) // 34 month fmt.Println(diff3) // 2 years 10 months
NewDiffWithMode returns error in the following cases:
start date is after end date
func (Diff) Equal ¶
Equal returns true when two dates differences are equal.
Example ¶
package main import ( "fmt" "time" "github.com/antklim/datediff" ) func main() { d1, _ := time.Parse("2006-01-02", "2000-10-01") d2, _ := time.Parse("2006-01-02", "2000-10-30") diff1, _ := datediff.NewDiff(d1, d2, "%Y %M %D") diff2, _ := datediff.NewDiff(d1, d2, "%Y %M %D") fmt.Println(diff1.Equal(diff2)) diff3, _ := datediff.NewDiff(d1, d2.Add(-48*time.Hour), "%Y %M %D") fmt.Println(diff1.Equal(diff3)) diff4, _ := datediff.NewDiff(d1, d2, "%Y") fmt.Println(diff1.Equal(diff4)) }
Output: true false false
func (Diff) Format ¶
Format formats dates difference accordig to provided format.
Example ¶
package main import ( "fmt" "time" "github.com/antklim/datediff" ) func main() { d1, _ := time.Parse("2006-01-02", "2000-10-01") d2, _ := time.Parse("2006-01-02", "2010-11-30") diff, _ := datediff.NewDiff(d1, d2, "%Y %M %D") s, _ := diff.Format("%y anos") fmt.Println(s) }
Output: 10 anos
func (Diff) FormatWithZeros ¶
FormatWithZeros formats dates difference accordig to provided format.
Example ¶
package main import ( "fmt" "time" "github.com/antklim/datediff" ) func main() { d1, _ := time.Parse("2006-01-02", "2000-10-01") d2, _ := time.Parse("2006-01-02", "2010-10-30") diff, _ := datediff.NewDiff(d1, d2, "%Y %M %D") s, _ := diff.FormatWithZeros("%y anos %m meses %d dias") fmt.Println(s) }
Output: 10 anos 0 meses 29 dias
func (Diff) String ¶
String formats dates difference according to the format provided at initialization of dates difference. Time units that have 0 value omitted.
Example ¶
package main import ( "fmt" "time" "github.com/antklim/datediff" ) func main() { d1, _ := time.Parse("2006-01-02", "2000-10-01") d2, _ := time.Parse("2006-01-02", "2010-11-30") { diff1, _ := datediff.NewDiff(d1, d2, "%Y") diff2, _ := datediff.NewDiff(d1, d2, "%Y %M") diff3, _ := datediff.NewDiff(d1, d2, "%Y %M %D") fmt.Println(diff1) fmt.Println(diff2) fmt.Println(diff3) } { diff1, _ := datediff.NewDiffWithMode(d1, d2, datediff.ModeYears) diff2, _ := datediff.NewDiffWithMode(d1, d2, datediff.ModeYears|datediff.ModeMonths) diff3, _ := datediff.NewDiffWithMode(d1, d2, datediff.ModeYears|datediff.ModeMonths|datediff.ModeWeeks) fmt.Println(diff1) fmt.Println(diff2) fmt.Println(diff3) } }
Output: 10 years 10 years 1 month 10 years 1 month 29 days 10 years 10 years 1 month 10 years 1 month 4 weeks
func (Diff) StringWithZeros ¶
StringWithZeros formats dates difference according to the format provided at initialization of dates difference. It keeps time units values that are 0.
Example ¶
package main import ( "fmt" "time" "github.com/antklim/datediff" ) func main() { d1, _ := time.Parse("2006-01-02", "2000-10-01") d2, _ := time.Parse("2006-01-02", "2010-10-30") diff, _ := datediff.NewDiff(d1, d2, "%Y %M %D") fmt.Println(diff.StringWithZeros()) }
Output: 10 years 0 months 29 days