SoFunction
Updated on 2025-03-05

Summary of daily usage of Golang standard library time package

Time and date are a data type that is often used in programming. Time package is provided in Golang to handle time-related operations. This tutorial will introduce in detail the examples of commonly used functions and methods in the time package.

  • (): Get the current time.
package main

import (
	"fmt"
	"time"
)

func main() {
	currentTime := ()
	("Current time:", currentTime)
 ("Current timestamp:", ())
}

Output:

Current time: 2023-09-16 10:36:49.054695 +0800 CST m=+0.011414701
Current timestamp: 1694834492

  • (): Create a time based on a given year, month, day, hour, minute, second, and nanosecond.
package main

import (
	"fmt"
	"time"
)

func main() {
	t := (2023, , 14, 18, 30, 0, 0, )
	("Specified time:", t)
}

Output:

Specified time: 2023-09-16 18:30:00 +0000 UTC

  • (): Parses the string to a time type.
package main

import (
	"fmt"
	"time"
)

func main() {
	layout := "2006-01-02"
	str := "2023-09-16"
	t, _ := (layout, str)
	("Time after parsing:", t)
}

Output:

Time after parsing: 2023-09-16 00:00:00 +0000 UTC

  • (): Represents a period of time intervals, which can be used to represent time units such as milliseconds, seconds, minutes, and hours.
package main

import (
	"fmt"
	"time"
)

func main() {
	duration := (2) * 
	("Time interval:", duration)
}

Output:

Time interval: 2s

  • (): Let the program sleep for a period of time.
package main

import (
	"fmt"
	"time"
)

func main() {
	("start")
	(2 * )
	("Finish")
}

Output:

start
// The program sleeps for 2 seconds
Finish

  • (): Create a timer and repeat an operation at the specified time interval.
package main

import (
	"fmt"
	"time"
)

func main() {
	ticker := (1 * )
	for range ticker {
		("Tick")
	}
}

Output:

Tick
Tick
Tick
...

  • (): Format time to the specified string form.
package main

import (
	"fmt"
	"time"
)

func main() {
	t := ()
	("Format time:", ("2006-01-02 15:04:05"))
}

Output:

Format time: 2023-09-16 10:41:26

Types are the standard type for processing dates and times in Go. It contains various methods for operation and calculation of time. Here are some commonly used methods:

1.(d Duration) : This method is used to add time interval d to time t. The type of parameter d isDuration, it represents a time period. AvailableEqual units represent the time interval. Sample code:

package main

import (
    "fmt"
    "time"
)

func main() {
    t := ()
    d := 10 * 
    newT := (d)

    ("Original Time:", t)
    ("Time after increasing the time interval:", newT)
}

Original time: 2023-09-16 10:42:05.113844 +0800 CST m=+0.008983801
Time after adding time interval: 2023-09-16 10:42:15.113844 +0800 CST m=+10.008983801

  • (u ) Duration: This method is used to calculate the time interval between time t and time u. The return value type isDuration, you can use itSeconds()、Minutes()、Hours()Use other methods to obtain the number of seconds, minutes, hours, etc. of the time interval. Sample code:
package main

import (
    "fmt"
    "time"
)

func main() {
    t1 := ()
    t2 := (2022, , 1, 0, 0, 0, 0, )
    duration := (t2)

    ("Time interval:", duration)
    ("Second:", ())
    ("Minutes:", ())
    ("Hours:", ())
}

Time interval: 6194h43m3.5463288s
Number of seconds: 2.23009835463288e+07
Minutes: 371683.05910548
Hours: 6194.717651758

3.(u ) bool: This method is used to determine whether the two times are equal. If the two times are equal, returntrue, otherwise returnfalse. Sample code:

package main

import (
    "fmt"
    "time"
)

func main() {
    t1 := ()
    t2 := ()
    t3 := (2023, , 1, 0, 0, 0, 0, )

    ("Is t1 and t2 equal?", (t2))
    ("Is t1 and t3 equal?", (t3))
}

Is t1 and t2 equal?
Are t1 and t3 equal?: false

  • (u ) bool: This method is used to determine whether time t is before time u. If time t is before time u, returntrue, otherwise returnfalse. Sample code:
package main

import (
    "fmt"
    "time"
)

func main() {
    t1 := ()
    t2 := (2023, , 1, 0, 0, 0, 0, )

    ("Is t1 before t2:", (t2))
}

Is t1 before t2: false

5.(u ) bool: This method is used to determine whether time t is after time u. If time t is after time u, returntrue, otherwise returnfalse. Sample code:

package main

import (
    "fmt"
    "time"
)

func main() {
    t1 := ()
    t2 := (2023, , 1, 0, 0, 0, 0, )

    ("Is t1 after t2:", (t2))
}

Whether t1 is after t2: true

The above is an introduction and sample code about some commonly used functions and methods in the time package. You can use them to obtain the current time, create a specified time, parse time strings, control time intervals and other operations. Using these functions and methods in light of actual needs can flexibly deal with time and date-related logic. Very practical. For more information about the usage of Golang time package, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!