10 examples of 'golang time difference' in Go

Every line of 'golang time difference' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Go code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
173func timeago(t int64) string {
174 const (
175 mm = 60
176 hh = 60 * mm
177 dd = 24 * hh
178 ww = 7 * dd
179 )
180 now := time.Seconds()
181 dif := now-t
182 if dif>dd {
183 if dif>ww { return time.SecondsToUTC(t).Format("2006/01/02")
184 } else {
185 if dif<dd*2 { return "Yesterday"
186 } else { return fmt.Sprintf("%d days ago",(int(dif/dd))) }
187 }
188 }
189 s := ""
190 if dif>=mm {
191 m := int(dif/mm)
192 if m>59 {
193 h := dif/hh
194 if h>1 { s="s" }
195 return fmt.Sprintf("%d hour%s ago",h,s)
196 }
197 if m>1 { s="s" }
198 return fmt.Sprintf("%d minute%s ago",m,s)
199 }
200 if dif<5 { return "just now" }
201 return fmt.Sprintf("%d seconds ago",dif)
202}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

149func diffTimes(a time.Time, b time.Time) interface{} {
150 if a == b {
151 return a
152 }
153 return fmt.Sprintf("%v . . . %v (%v difference)", a, b, b.Sub(a))
154}
1018func TimeDelta(cefTime1, cefTime2 *Time, delta *int64) int32 {
1019 return int32(C.cef_time_delta(cefTime1.toNative(&C.cef_time_t{}), cefTime2.toNative(&C.cef_time_t{}), (*C.longlong)(delta)))
1020}
1152func (s *Delete_commandContext) Time() ITimeContext {
1153 var t = s.GetTypedRuleContext(reflect.TypeOf((*ITimeContext)(nil)).Elem(), 0)
1154
1155 if t == nil {
1156 return nil
1157 }
1158
1159 return t.(ITimeContext)
1160}
360func timeDiff(now, prev int64) time.Duration {
361 return time.Duration((now - prev) * time.Nanosecond.Nanoseconds())
362}
6func main() {
7 // Create new date.
8 a := time.Date(2018, 4, 11, 0, 0, 0, 0, time.UTC)
9
10 delta := time.Now().Sub(a)
11 fmt.Println(delta.Hours())
12
13 duration := time.Since(a)
14 fmt.Println(duration.Hours())
15}
38func (d *goDate) DifferenceAsDuration(compare *goDate) time.Duration {
39 return d.Time.Sub(compare.Time)
40}
34func minDuration(d1, d2 time.Duration) time.Duration {
35 if d1 < d2 {
36 return d1
37 }
38 return d2
39}
120func timeAgo(ago time.Duration) string {
121 if ago < time.Minute {
122 return "just now"
123 }
124 if ago < time.Hour {
125 return utils.Pluralize(int(ago.Minutes()), "minute") + " ago"
126 }
127 if ago < 24*time.Hour {
128 return utils.Pluralize(int(ago.Hours()), "hour") + " ago"
129 }
130 if ago < 30*24*time.Hour {
131 return utils.Pluralize(int(ago.Hours())/24, "day") + " ago"
132 }
133 if ago < 365*24*time.Hour {
134 return utils.Pluralize(int(ago.Hours())/24/30, "month") + " ago"
135 }
136 return utils.Pluralize(int(ago.Hours()/24/365), "year") + " ago"
137}
117func formatTimedelta(d int64) string {
118 u := uint64(d)
119 neg := d < 0
120 if neg {
121 u = -u
122 }
123 secs := u % 60
124 u /= 60
125 mins := u % 60
126 u /= 60
127 hours := u % 24
128 days := u / 24
129
130 if days == 0 {
131 return fmt.Sprintf("%02d:%02d:%02d", hours, mins, secs)
132 } else {
133 return fmt.Sprintf("%dd ", days) + fmt.Sprintf("%02d:%02d:%02d", hours, mins, secs)
134 }
135}

Related snippets