C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Year: With year we get an int equal to the year field in the Time struct. Here it returns 2015.
Month: This returns the month of the time struct as an int. When we use Println, its String method displays it in a readable way.
Day: This is the day field of the Time struct—not the total number of days in the time.
Golang program that uses time, Year, Month, Day
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())
}
Output
2017-02-21 15:08:12.0658324 -0800 PST
2017
February
21
Return: Parse() returns two things: the Time struct and an error (if any). We can use the blank identifier to ignore the error.
Quote: Parse parses a formatted string and returns the time value it represents. The layout defines the format by showing how the reference time, defined to be Mon Jan 2 15:04:05 -0700 MST 2006 would be interpreted if it were the value.
Time: golang.orgGolang program that uses time, Parse
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)
}
Output
2015-01-28 00:00:00 +0000 UTC
Seconds: This is a method on the duration that is returned by Sub. It is a floating-point number.
Minutes: This is like seconds, but divided by 60. It is mainly a convenience method.
Golang program that uses Sub, Duration, Seconds, Minutes
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)
}
Output
4999950000
Duration 1.8052613s
Seconds 1.8052613000000002
Minutes 0.030087688333333334
Tip: Don't pass nil as the Location (the last argument). This will cause a panic. I found this out the hard way.
Golang program that uses time.Date to create Time
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)
}
Output
2006-01-01 12:00:00 +0000 UTC
Golang program that uses Before, After
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))
}
Output
true
true
false
Note: Equal() is a more conceptual time comparison, which accounts for Locations. The "==" operator just compares two Time instances.
Golang program that compares times with Equal
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)
}
}
Output
Times are equal
true
Duration: We construct the Duration from nanoseconds. One million nanoseconds is one millisecond. We multiply that by 100 for 100 ms.
Golang program that uses Sleep and Duration
package main
import (
"fmt"
"time"
)
func main() {
for i := 0; i < 4; i++ {
// Duration receives nanoseconds.
// ... 100 million nanoseconds is 100 milliseconds.
d := time.Duration(1000 * 1000 * 100)
// Sleep for 100 ms.
time.Sleep(d)
fmt.Println(time.Now())
}
}
Output
2015-07-25 15:59:42.3227647 -0700 PDT
2015-07-25 15:59:42.4247739 -0700 PDT
2015-07-25 15:59:42.5249032 -0700 PDT
2015-07-25 15:59:42.6249464 -0700 PDT
Here: We convert the result of Now() to a simple date format. The Format method returns a string.
Golang program that uses Format
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)
}
Output
Jul 27, 2015
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.
Golang program that uses time.Since func
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)
}
Output
2018-01-01 00:00:00 +0000 UTC
Days passed: 728.0187105963055