SoFunction
Updated on 2025-03-02

Introduction to the usage of DateTime in Go language

1. Basic use

① Subordinate to the time package

② Generally use

This type represents time, and there are some constants in the time package, the source code is as follows

// Common durations. There is no definition for units of Day or larger
// to avoid confusion across daylight savings time zone transitions.
//
// To count the number of units in a Duration, divide:
//  second := 
//  (int64(second/)) // prints 1000
//
// To convert an integer number of units to a Duration, multiply:
//  seconds := 10
//  ((seconds)*) // prints 10s
//<br data-filtered="filtered">
const (
    Nanosecond  Duration = 1
    Microsecond          = 1000 * Nanosecond
    Millisecond          = 1000 * Microsecond
    Second               = 1000 * Millisecond
    Minute               = 60 * Second
    Hour                 = 60 * Minute
)

③ () Get the current time, the returned Time type

In Time type

  • Year() Gets the current year
  • Month() Gets the current month
  • Day() Gets the current date
  • Hour() Gets the current hour
  • Minute() Gets the current minute
  • Second() Gets the current second

④ Commonly used Unix() methods to obtain timestamp information

⑤Addate() method adds the specified date, and the Add method adds the specified time

For example:

  • ① Print time basic information
func DateTimeBasic() {
    now:=()
 
    ("%v\n",now)
 
    year:=()
    month:=()
    day:=()
    hour:=()
    minute:=()
    send:=()
 
    ("%02d/%02d/%02d %02d:%02d:%02d\n",year,month,day,hour,minute,send)
 
    return now
}
  • ② Direct conversion of time and timestamps The first function inputs time and returns the time stamp, and the second function inputs time stamps and returns the time stamps.
func GetDateTimeStamp(datetime ) int64{
    now:=()
    ("TimeStamp: %v\n",now)
    return now
}
 
func GetDateTime(timeStamp int64){
    now:=(timeStamp,0)
    ("DateTime: %v\n",now)
}
  • ③Add time
func AddDay(src ) {
    //Add two hours a day    src = (0,0,1).Add( * 2)
    return src
}
  • ④Test
package main
 
import "DateTimeDemo"
 
func main(){
     
    dateTime:=()
    calcDateTime := (dateTime)
    timeStamp:=(calcDateTime)
    (timeStamp)
     
}
  • ⑤Output

2019-02-26 16:51:59.7509972 +0800 CST m=+0.010553801
2019/02/26 16:51:59
TimeStamp: 1551264719
DateTime: 2019-02-27 18:51:59 +0800 CST

2. Simple timer

Use the Tick() method in time

func SimpleTicker(){
   //Add two seconds apart will write a data like a Channel   ticker := ( * 2)
 
   for i := range ticker{
      ("%v\n",i)
      simpleTask()
   }
}
 
func simpleTask(){
   ("Task Start")
}

Execution results:

2019-02-26 16:54:43.7828451 +0800 CST m=+2.077803401
Task Start
2019-02-26 16:54:45.7831559 +0800 CST m=+4.078114201
Task Start
2019-02-26 16:54:47.7831744 +0800 CST m=+6.078132701
Task Start
2019-02-26 16:54:49.7833155 +0800 CST m=+8.078273801
Task Start
2019-02-26 16:54:51.782682 +0800 CST m=+10.077640301
Task Start

The above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support. If you want to know more about it, please see the following links