SoFunction
Updated on 2025-03-05

GoLang Time Time Operation Function Explanation

time package

The time package provides functions for displaying and measuring time. Calendar calculation uses the Gregorian calendar.

Time Type

Type indicates time. We can pass()The function obtains the current time object, and then obtains the time object's year, month, day, time, minute, and second information. The sample code is as follows:

package main
import (
	"fmt"
	"time"
)
func main() {
	// Get the current time 2022-07-19 10:03:42.70981 +0800 CST m=+0.000080935	now := ()
	(now)
	(())
	(())
	(())
	(())
	(())
	(())
}

Timestamp

The timestamp is the total number of milliseconds from January 1, 1970 (08:00:00GMT) to the current time. It is also called Unix Timestamp.

The example code for obtaining a timestamp based on a time object is as follows:

package main
import (
	"fmt"
	"time"
)
func main() {
	// Get the current time 2022-07-19 10:03:42.70981 +0800 CST m=+0.000080935	now := ()
	(now)
	// Timestamp Second	(())
	// Timestamp milliseconds	(())
	// Timestamp Nanoseconds	(())
}

use()Functions can convert timestamps to time format.

package main
import (
	"fmt"
	"time"
)
func main() {
	// Get the current time 2022-07-19 10:03:42.70981 +0800 CST m=+0.000080935	now := ()
	(now)
	// Time stamp to time type	unix := ((), 0)
	(unix)
}

Time interval

yestimeA type defined by the package that represents the time elapsed between two time points, in nanoseconds.It represents a period of time interval, and the maximum time period that can be represented is about 290 years.

timeThe constants of the time interval type defined in the package are as follows:

For example: means 1 nanosecond, 1 second.

const (
    Nanosecond  Duration = 1
    Microsecond          = 1000 * Nanosecond
    Millisecond          = 1000 * Microsecond
    Second               = 1000 * Millisecond
    Minute               = 60 * Second
    Hour                 = 60 * Minute
)

At the same time, a method is provided to obtain the value of the granularity of each time

func (d Duration) Nanoseconds() int64 {}   // Nanosecondsfunc (d Duration) Microseconds() int64 {}  // Microsecondsfunc (d Duration) Milliseconds() int64 {}  // millisecondsfunc (d Duration) Seconds() float64 {}     // Secondfunc (d Duration) Minutes() float64 {}     // minutefunc (d Duration) Hours() float64 {}       // Hour

Time operation

AddThe function is used to increase/decrease the duration of (the positive value of d indicates increase and negative value indicates decrease). For a certain instantaneous time, increase or decrease the time of the specified nanosecond level or more.

func (t Time) Add(d Duration) Time {}

Sub The function can obtain the duration between two time instants.

func (t Time) Sub(u Time) Duration {}

AddDateThe function is based on the value of the increase/decrease of the dimensions of the year, month, and day.

func (t Time) AddDate(years int, months int, days int) Time {}

Of course, based on the current time moment() The calculation is the most common requirement. therefore,time The package also provides the following convenient time calculation functions.

SinceThe function is().Sub(t)quick way.

func Since(t Time) Duration {}

UntilThe function is(())quick way.

func Until(t Time) Duration {}

Example of usage

 t := ()
 (t)                      // 2022-07-17 22:41:06.001567 +0800 CST m=+0.000057466
 //Time increases by 1 hour (( * 1))   // 2022-07-17 23:41:06.001567 +0800 CST m=+3600.000057466
 //Time increases by 15 minutes (( * 15))// 2022-07-17 22:56:06.001567 +0800 CST m=+900.000057466
 //Time increases by 10 seconds (( * 10))// 2022-07-17 22:41:16.001567 +0800 CST m=+10.000057466
 //Time decreases by 1 hour ((- * 1))  // 2022-07-17 21:41:06.001567 +0800 CST m=-3599.999942534
 //Time decreases by 15 minutes ((- * 15))// 2022-07-17 22:26:06.001567 +0800 CST m=-899.999942534
 //Time decreases by 10 seconds ((- * 10))// 2022-07-17 22:40:56.001567 +0800 CST m=-9.999942534
 ( * 5)
 t2 := ()
 // Calculate the duration from t to t2 ((t))              // 5.004318874s
 // 1 year later t3 := (1, 0, 0)
 // Calculate the duration from t to the current ((t))          // 5.004442316s
 // Calculate the duration from now to next year ((t3))         // 8759h59m59.999864s

Time formatting

There is a method for the time typeFormatFor formatting, in other languages, common time templates are generally used to format the time. For example, Python uses %Y for year, %m for month, %d for day, etc. It should be noted that formatting time templates in Go are not commonY-m-d H:M:SInstead, it was born at 15:04 on January 2, 2006 (the memory formula is 2006 1 2 3 4). Maybe this is the romance of technicians.

Added: If you want to format it into a 12-hour mode, you need to specify the PM.

FormateFunctions are used toThe object is converted to a time string according to the given layout.

    now := ()
    // The formatted template is Go's birth date January 2, 2006 at 15:04 Mon Jan    // 24-hour system    (("2006-01-02 15:04:05.000 Mon Jan"))
    // 12-hour system    (("2006-01-02 03:04:05.000 PM Mon Jan"))
    (("2006/01/02 15:04"))
    (("15:04 2006/01/02"))
    (("2006/01/02"))

ParseFunctions are used to convert a time string into a corresponding layout according to itsObject.

now := ()
(now)
// Loading time zoneloc, err := ("Asia/Shanghai")
if err != nil {
    (err)
    return
}
// Parse string time according to the specified time zone and the specified formattimeObj, err := ("2006/01/02 15:04:05", "2019/08/04 14:15:20", loc)
if err != nil {
    (err)
    return
}
(timeObj)
((now))

This is the end of this article about the explanation of GoLang Time time operation function. For more related Go Time content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!