Operations related to dates, time, and durations.
Time
Examples in
Go
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now()
fmt.Println(now)
}
package main
import (
"fmt"
"time"
)
func main() {
p := fmt.Println
then := time.Date(
2009, 11, 17, 20, 34, 58, 651387237, time.UTC)
p(then)
p(then.Year())
p(then.Month())
p(then.Day())
p(then.Hour())
p(then.Minute())
p(then.Second())
p(then.Nanosecond())
p(then.Location())
p(then.Weekday())
}
package main
import (
"fmt"
"time"
)
func main() {
p := fmt.Println
now := time.Now()
then := time.Date(
2009, 11, 17, 20, 34, 58, 651387237, time.UTC)
p(then.Before(now))
p(then.After(now))
p(then.Equal(now))
}
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now()
fmt.Println(now.Year())
}
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now()
// Pretty Print.
fmt.Println(now.Month())
// Integer value.
fmt.Println(int(now.Month()))
// Compare with a month enum constant.
fmt.Println(now.Month() == time.January)
}
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now()
year, week := now.ISOWeek()
fmt.Println(year, week)
}
Hour returns the hour within the day specified by t, in the range [0, 23].
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now()
fmt.Println(now.Hour())
}
Minute returns the minute offset within the hour specified by t, in the range [0, 59].
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now()
fmt.Println(now.Minute())
}
Second returns the second offset within the minute specified by t, in the range [0, 59].
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now()
fmt.Println(now.Second())
}
package main
import (
"fmt"
"time"
)
func main() {
p := fmt.Println
now := time.Now()
then := time.Date(
2009, 11, 17, 20, 34, 58, 651387237, time.UTC)
// The Sub methods returns a Duration representing the interval between two times.
diff := now.Sub(then)
p(diff)
p(diff.Hours())
p(diff.Minutes())
p(diff.Seconds())
p(diff.Nanoseconds())
}
package main
import (
"fmt"
"time"
)
func main() {
p := fmt.Println
now := time.Now()
then := time.Date(
2009, 11, 17, 20, 34, 58, 651387237, time.UTC)
diff := now.Sub(then)
p(then.Add(diff))
p(then.Add(-diff))
}
A common requirement in programs is getting the number of seconds, milliseconds, or nanoseconds since the Unix epoch. Here’s how to do it in Go.
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now()
secs := now.Unix()
nanos := now.UnixNano()
fmt.Println(now)
// Note that there is no UnixMillis, so to get the milliseconds since epoch
// you'll need to manually divide from nanoseconds.
millis := nanos / 1000000
fmt.Println(secs)
fmt.Println(millis)
fmt.Println(nanos)
// You can also convert integer seconds or nanoseconds since the epoch into the corresponding time.
fmt.Println(time.Unix(secs, 0))
fmt.Println(time.Unix(0, nanos))
}
package main
import (
"fmt"
"time"
)
func main() {
p := fmt.Println
// Here's a basic example of formatting a time according to RFC3339,
// using the corresponding layout constant.
t := time.Now()
p(t.Format(time.RFC3339))
// Layouts must use the reference time Mon Jan 2 15:04:05 MST 2006 to show the pattern
// with which to format a given time/string.
p(t.Format("3:04PM"))
p(t.Format("Mon Jan _2 15:04:05 2006"))
p(t.Format("2006-01-02T15:04:05.999999-07:00"))
// For purely numeric representations you can also use standard string formatting
// with the extracted components of the time value.
fmt.Printf("%d-%02d-%02dT%02d:%02d:%02d-00:00\n",
t.Year(), t.Month(), t.Day(),
t.Hour(), t.Minute(), t.Second())
}
package main
import (
"fmt"
"time"
)
func main() {
p := fmt.Println
t1, e := time.Parse(
time.RFC3339,
"2012-11-01T22:08:41+00:00")
p(t1)
form := "3 04 PM"
t2, e := time.Parse(form, "8 41 PM")
p(t2)
// Parse will return an error on malformed input explaining the parsing problem.
ansic := "Mon Jan _2 15:04:05 2006"
_, e = time.Parse(ansic, "8:41PM")
p(e)
}
Last Run
:
2012-11-01 22:08:41 +0000 UTC
0000-01-01 20:41:00 +0000 UTC
parsing time "8:41PM" as "Mon Jan _2 15:04:05 2006": cannot parse "8:41PM" as "Mon"
Nanosecond returns the nanosecond offset within the second specified by t, in the range [0, 999999999].
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now()
fmt.Println(now.Nanosecond())
}
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now()
fmt.Println(now.Nanosecond() / 1000000)
}
Last Run
:
876
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now()
fmt.Println(now.Nanosecond() / 1000)
}
Hours returns the duration as a floating point number of hours.
package main
import (
"fmt"
"time"
)
func main() {
h, _ := time.ParseDuration("4h30m")
fmt.Printf("I've got %.1f hours of work left.", h.Hours())
}
Last Run
:
I've got 4.5 hours of work left.
Minutes returns the duration as a floating point number of minutes.
package main
import (
"fmt"
"time"
)
func main() {
m, _ := time.ParseDuration("1h30m")
fmt.Printf("The movie is %.0f minutes long.", m.Minutes())
}
Last Run
:
The movie is 90 minutes long.
Seconds returns the duration as a floating point number of seconds.
package main
import (
"fmt"
"time"
)
func main() {
m, _ := time.ParseDuration("1m30s")
fmt.Printf("Take off in t-%.0f seconds.", m.Seconds())
}
Last Run
:
Take off in t-90 seconds.
package main
import (
"fmt"
"time"
)
func main() {
m, _ := time.ParseDuration("1m30s")
fmt.Printf("%d millseconds.", m.Milliseconds())
}
Last Run
:
90000 millseconds.
package main
import (
"fmt"
"time"
)
func main() {
m, _ := time.ParseDuration("1m30s")
fmt.Printf("%d microseconds.", m.Microseconds())
}
Last Run
:
90000000 microseconds.
Nanoseconds returns the duration as an integer nanosecond count.
package main
import (
"fmt"
"time"
)
func main() {
u, _ := time.ParseDuration("1µs")
fmt.Printf("One microsecond is %d nanoseconds.\n", u.Nanoseconds())
}
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now()
// Day name.
fmt.Println(now.Weekday())
// Day integer.
fmt.Println(int(now.Weekday()))
// Compare with enum.
fmt.Println(now.Weekday() == time.Monday)
}
YearDay returns the day of the year specified by t, in the range [1,365] for non-leap years, and [1,366] in leap years.
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now()
fmt.Println(now.YearDay())
}
Day returns the day of the month specified by t.
package main
import (
"fmt"
"time"
)
func main() {
d := time.Date(2000, 2, 1, 12, 30, 0, 0, time.UTC)
day := d.Day()
fmt.Printf("day = %v\n", day)
}