Home
Go
time: Now, Parse and Duration
Updated Feb 8, 2025
Dot Net Perls
Time. Programs often handle times and dates. They read strings in from files, parse them, test them for validity. Times are changed.
With the time package, we access many usable funcs on the Time struct. These methods are reusable and tested—this is a clear advantage.
Sleep
Now example. Let us start with this simple example. We import the "time" package in the import block at the top. We invoke the Now method from the time package. It returns a struct.
Info With Year we get an int equal to the year field in the Time struct. Here it returns 2015.
Next Month returns the month of the time struct as an int. When we use Println, its String method displays it in a readable way.
Finally We access the day field of the Time struct. This is not the same as the total number of days in the time.
package main import ( "fmt" "time" ) func main() { // Get the current time. t := time.Now() fmt.Println(t) // Print year, month and day of the current time. fmt.Println(t.Year()) fmt.Println(t.Month()) fmt.Println(t.Day()) }
2017-02-21 15:08:12.0658324 -0800 PST 2017 February 21
Parse. With this func we convert a string to a Time struct. Parse receives two strings: a form string and the value we are parsing.
Return Parse() returns two things: the Time struct and an error (if any). We can use the blank identifier to ignore the error.
package main import ( "fmt" "time" ) func main() { // This is the value we are trying to parse. value := "January 28, 2015" // The form must be January 2,2006. form := "January 2, 2006" // Parse the string according to the form. t, _ := time.Parse(form, value) fmt.Println(t) }
2015-01-28 00:00:00 +0000 UTC
Sub, Duration. This example combines many time concepts. It uses Now() twice to measure the time required to run a piece of code. Then it uses Sub() to get a duration.
Info Seconds() is a method on the duration that is returned by Sub. It is a floating-point number.
Also Minutes is like seconds, but divided by 60. It is mainly a convenience method.
package main import ( "fmt" "time" ) func main() { t0 := time.Now() // Do a slow operation. count := 0 for i := 0; i < 100000; i++ { for x := 0; x < i; x++ { count++ } } t1 := time.Now() // Display result. fmt.Println(count) // Get duration. d := t1.Sub(t0) fmt.Println("Duration", d) // Get seconds from duration. s := d.Seconds() fmt.Println("Seconds", s) // Get minutes from duration. m := d.Minutes() fmt.Println("Minutes", m) }
4999950000 Duration 1.8052613s Seconds 1.8052613000000002 Minutes 0.030087688333333334
Create Time, time.Date. To create a Time from ints (like the year, month and day) we call the time.Date func. The time.Date func returns a new Time instance.
Tip Don't pass nil as the Location (the last argument). This will cause a panic. I found this out the hard way.
package main import ( "fmt" "time" ) func main() { // Create a Date in 2006. t := time.Date(2006, 1, 1, 12, 0, 0, 0, time.UTC) fmt.Println(t) }
2006-01-01 12:00:00 +0000 UTC
Before, After. Does one Time come before another? With the Before() func we can determine temporal order. And with After(), we can test whether one time comes after another.
package main import ( "fmt" "time" ) func main() { // T1 is a time earlier than t2. t1 := time.Date(2000, 1, 1, 12, 0, 0, 0, time.UTC) t2 := time.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC) // This is true: the first time is before the second. if t1.Before(t2) { fmt.Println(true) } // This is true. if t2.After(t1) { fmt.Println(true) } // This returns false. fmt.Println(t1.After(t2)) }
true true false
Equal. With the Equal() func we see if two times indicate the same instant. Equal() adjusts for Locations. With the "==" operator, the Locations must also be equal.
Note Equal() is a more conceptual time comparison, which accounts for Locations. The "==" operator just compares two Time instances.
package main import ( "fmt" "time" ) func main() { // These times are created with equal values. t1 := time.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC) t2 := time.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC) // Compare times. if t1 == t2 { fmt.Println("Times are equal") } // Equal adjusts for locations. if t1.Equal(t2) { fmt.Println(true) } }
Times are equal true
Format. With Format we convert a Date into a string. As with Parse, we use the special date Jan 2, 2006 to indicate the formatting.
Here We convert the result of Now() to a simple date format. The Format method returns a string.
Filename, Date
package main import ( "fmt" "time" ) func main() { // This date is used to indicate the layout. const layout = "Jan 2, 2006" // Format Now with the layout const. t := time.Now() res := t.Format(layout) fmt.Println(res) }
Jul 27, 2015
Since. The Since() func is an easy way to compute the elapsed hours from a point in time. We do not need to use Now and Sub separately—Since() includes this logic for us.
Part 1 We create a new Date with time.Date. We specify 8 arguments to time.Date for this purpose.
Part 2 We invoke the time.Since() func and pass the date we just created to it. Time.Since accesses the current time.
Part 3 The program displays how many days have passed since the specified date in January 2018.
package main import ( "fmt" "time" ) func main() { // Part 1: get first day of 2018. t := time.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC) fmt.Println(t) // Part 2: compute days since first day of 2018. timePassed := time.Since(t) // Part 3: display result. fmt.Println("Days passed:", timePassed.Hours() / 24) }
2018-01-01 00:00:00 +0000 UTC Days passed: 728.0187105963055
Benchmarks. The time module can be used to take benchmarks. We use Sub to get a difference (a duration) between two times returned by Now calls.
package main import ( "fmt" "time" ) const MAX = 10000000 func main() { t0 := time.Now() // Version 1: describe version. for i := 0; i < MAX; i++ { } t1 := time.Now() // Version 2: describe version. for i := 0; i < MAX; i++ { } t2 := time.Now() // Benchmark results. fmt.Println(t1.Sub(t0)) fmt.Println(t2.Sub(t1)) }
3.265ms 5.881125ms
With the Time struct, we gain a way to represent a point in time (a Date). And with the methods in time, we can manipulate it. We cannot go back in time, but we can represent it.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Feb 8, 2025 (new example).
Home
Changes
© 2007-2025 Sam Allen