SoFunction
Updated on 2025-03-05

Golang gets the current time code

Time-related operations in golang mainly use functions packaged in time, and this object is mainly included in time.

1. Get the current time

(1) currentTime:=()     //Get the current time, the time type of Go is Time

  (2)

t1:=().Year()          //Year
t2:=().Month()        //month
t3:=().Day()            //Day
t4:=().Hour()         //Hour
t5:=().Minute()       //Minute
t6:=().Second()      //Second
t7:=().Nanosecond() //nanosecond
 
currentTimeData:=(t1,t2,t3,t4,t5,t6,t7,) //Get the current time and return the current time Time
 
(currentTime)       //Print result: 2017-04-11 12:52:52.794351777 +0800 CST
(t1,t2,t3,t4,t5,t6)     //Print result: 2017 April 11 12 52 52
(currentTimeData)    //Print result: 2017-04-11 12:52:52.794411287 +0800 CST

now := ().Unix() //Get the timestamp
().UTC() //Convert to UTC time
(10 * ) //time's sleep

Example sharing:

Get the current time

func getNow() {
// Get the current time and return the object
    (())
    // output: 2016-07-27 08:57:46.53277327 +0800 CST
// where CST can be regarded as the standard time in the United States, Australia, Cuba or China
// +0800 means 8 hours faster than UTC time
 
// Get the current time stamp
    (().Unix())
// Accurately to nanoseconds, milliseconds and subtleties can be calculated through nanoseconds
    (().UnixNano())
    // output:
    //    1469581066
    //    1469581438172080471
}

Format time display

func formatUnixTime() {
// Get the current time and format it
    (().Format("2006-01-02 15:04:05"))
    // output: 2016-07-27 08:57:46
 
// Format the specified time
    ((1469579899, 0).Format("2006-01-02 15:04:05"))
    // output: 2016-07-27 08:38:19
}

Get the year for the specified timestamp

func getYear() {
// Get the year, month, day, hour, minute, and second
    t := (1469579899, 0)
    ("%d-%d-%d %d:%d:%d\n", (), (), (), (), (), ())
    // output: 2016-7-27 8:38:19
}

Time String Convert Timestamp

// Convert time stamps such as time string at 2016-07-27 08:46:15
func strToUnix() {
// First analyze the time string, if it is correct, an object will be obtained
// You can use the object's function Unix to obtain it later
    t2, err := ("2006-01-02 15:04:05", "2016-07-27 08:46:15")
    if err != nil {
        (err)
    }
    (t2)
    (())
    // output:
    //     2016-07-27 08:46:15 +0000 UTC
    //     1469609175
}

Get the time stamp from the day starting from the time stamp

// Get the time stamp starting from the day based on the timestamp
// This will be used frequently in statistics
// The method is to use the timestamp to get a time format like 2016-01-01 00:00:00
// Then convert it to timestamp and it's OK
// Get the monthly start time and year start time similar
func getDayStartUnix() {
    t := (1469581066, 0).Format("2006-01-02")
    sts, err := ("2006-01-02", t)
    if err != nil {
        (err)
    }
    (())
    // output: 1469577600
}

Sleep

// Sleep
func sleep() {
// Sleep for 1 second
//     Indicates 1 millisecond
//      Indicates 1 subtle
//     Indicates 1 nanosecond
    (1 * )
// Sleep for 100 milliseconds
    (100 * )
 
}