AppDividend
Latest Code Tutorials

Golang Time Example | Time Package in Golang

0

Golang offers extensive support for times and durations. Go provides a built-in time package. Working with time in Go is pretty straightforward. The time package provides api for measuring and displaying time. The calendrical calculations always assume the Gregorian calendar, with no leap seconds.

Golang Time Example

There came a day in development when you needed to work with time. Every programming language provides us the full support of date and time because we always need to convert from one format to another.

Now, let’s import the time package and print the current time in the console.

Get current time in Go

// hello.go

package main

import (
	"fmt"
	"time"
)

func main() {
	p := fmt.Println
	now := time.Now()
	p(now)
}

Output

➜  hello go run hello.go
2019-12-03 16:16:28.95699 +0530 IST m=+0.000586222
➜  hello

We have used the Now() function to print the current time.

Operating systems provide both the “wall clock,” which is subject to changes for the clock synchronization and a “monotonic clock,” which is not.

The general rule of thumb is that the wall clock is for telling time, and the monotonic clock is for measuring the time.

Rather than split the API, in this package, the Time returned by time.Now() function contains both the wall clock reading and the monotonic clock reading; later time-telling operations use the wall clock reading, but later the time-measuring operations, particular comparisons and subtractions, we can use the monotonic clock reading.

Print specific date-time in Go

See the following code.

// hello.go

package main

import (
	"fmt"
	"time"
)

func main() {
	p := fmt.Println
	then := time.Date(2013, 11, 20, 11, 30, 58, 100585237, time.UTC)
	p(then)
}

Output

➜  hello go run hello.go
2013-11-20 11:30:58.100585237 +0000 UTC
➜  hello

You can build the time struct by providing the year, month, day, etc. Times are always associated with a Location, i.e., time zone.

You can extract the various components of the time value as expected. See the following code.

// hello.go

package main

import (
	"fmt"
	"time"
)

func main() {
	print := fmt.Println
	then := time.Date(2013, 11, 20, 11, 30, 58, 100585237, time.UTC)
	print(then.Year())
	print(then.Month())
	print(then.Day())
	print(then.Hour())
	print(then.Minute())
	print(then.Second())
	print(then.Nanosecond())
	print(then.Location())
}

Output

➜  hello go run hello.go
2013
November
20
11
30
58
100585237
UTC
➜  hello

Get weekdays in Go

We can also get the weekdays from Monday-Sunday.

// hello.go

package main

import (
	"fmt"
	"time"
)

func main() {
	print := fmt.Println
	then := time.Date(2013, 11, 20, 11, 30, 58, 100585237, time.UTC)
	print(then.Weekday())
}

Output

➜  hello go run hello.go
Wednesday
➜  hello

Compare times in Go

We can use the following three methods to compare the times as seconds, respectively.

  1. Before()
  2. Equal()
  3. After()

See the followingn code.

// hello.go

package main

import (
	"fmt"
	"time"
)

func main() {
	print := fmt.Println
	now := time.Now()
	then := time.Date(2013, 11, 20, 11, 30, 58, 100585237, time.UTC)
	print(then.Before(now))
	print(then.After(now))
	print(then.Equal(now))
}

Output

➜  hello go run hello.go
true
false
false
➜  hello

We are comparing the current time to that 20th of November time as seconds.

Time Durations in Go

We can add thirty minutes in the current time.

But for that, we need to convert the minutes into seconds and then add those seconds into the current time. See the following code.

// hello.go

package main

import (
	"fmt"
	"time"
)

func main() {
	print := fmt.Println
	now := time.Now()
	mins := 30
	later := now.Add(time.Duration(mins) * time.Minute)
	print(later)
}

Output

➜  hello go run hello.go
2019-12-03 17:15:21.554469 +0530 IST m=+1800.000423700
➜  hello

My OS’s current time is 16:45:19 and time.Add() function has added 30 minutes so, our later time will be around 17:15:21.

So, we have successfully added 30 minutes to our current time.

We can also subtract the time as well. See the following code.

// hello.go

package main

import (
	"fmt"
	"time"
)

func main() {
	print := fmt.Println
	now := time.Now()
	then := time.Date(2013, 11, 20, 11, 30, 58, 100585237, time.UTC)
	diff := now.Sub(then)
	print(diff)
	print(diff.Hours())
	print(diff.Minutes())
	print(diff.Seconds())
}

Output

➜  hello go run hello.go
52895h49m30.052540763s
52895.82501459466
3.1737495008756793e+06
1.9042497005254075e+08
➜  hello

Time Formatting in Golang

Various formatting options are pre-defined. See the following code.

// hello.go

package main

import (
	"fmt"
	"time"
)

func main() {
	print := fmt.Println
	now := time.Now()
	then := time.Date(2013, 11, 20, 11, 30, 58, 100585237, time.UTC)

	print(now.Format(time.RFC822))
	print(then.Format(time.Kitchen))
}

Output

➜  hello go run hello.go
03 Dec 19 16:54 IST
11:30AM
➜  hello

Parsing Time

You also use layouts for parsing strings representing times. See the following code.

// hello.go

package main

import (
	"fmt"
	"time"
)

func main() {
	print := fmt.Println
	const shortFormDate = "2006-01-02"
	loc, _ := time.LoadLocation("America/Chicago")
	t, _ := time.ParseInLocation(shortFormDate, "2015-01-19", loc)
	print(t)
}

Output

➜  hello go run hello.go
2015-01-19 00:00:00 -0600 CST
➜  hello

Conclusion

In this example, we have seen how to get the current time, format time, compare times, add and subtract times, and to work with time durations.

Finally, Golang Time Example | Time Package in Golang tutorial is over.

See also

Golang Http example

Golang flag example

Golang JSON example

Golang log example

Golang template example

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.