Get the current time and its seconds, milliseconds, and nanoseconds
now := () //Get the current time==>2019-08-21 11:30:51.2470317 +0800 CST m=+0.004501101 ("Timestamp (seconds): %v;\n", ().Unix()) //10 bits("Timestamp (nanosecond): %v;\n",().UnixNano()) //19 bits("Timestamp (milliseconds): %v;\n",().UnixNano() / 1e6) //or 1000 seconds can also be("Timestamp (nanoseconds-->seconds): %v;\n",().UnixNano() / 1e9)
The difference between two times is, the first time is t1 and the second time is t2, so the time difference is (t1)
The time display time can be set through Format() printing time format, as well as In interface + Location
For example:
TimeLocation, err := ("Asia/Shanghai") //err processing().In(TimeLocation).Format()
Get the time difference code of two time points
package main import ( "fmt" "time" ) func main() { temp := 0 t1 := () for i := 0; i < 100000; i++ { for j := 0; j < 10000; j++ { temp++ } } (temp) t2 := () ((t1)) }
Get the time before the specified time
// Get the time 50 seconds ago, method 1st,_ := ("-50s") ("50 seconds ago:",().Add(st)) // Get the time 1 minute ago, n seconds ago is * -n, method 2t := ().Add( * -1) ("One minute ago:",t) //Get the time 1 hour agosth,_ := ("-1h") ("1 hour ago:",().Add(sth)) // Get the time 2 days agooldTime := ().AddDate(0, 0, -2) //Get the time two months agooldTime := ().AddDate(0, -2, 0)
Get the time after the specified time
// Get the time after 50 seconds, method 1st,_ := ("50s") ("Time after 50 seconds:",().Add(st)) // Get the time after 1 minute, n seconds before * n, method 2t := ().Add( * 1) ("Time in one minute:",t) //Get the time after 1 hoursth,_ := ("1h") ("Time after 1 hour:",().Add(sth)) // Get the time after the current time 2 daysnewTime := ().AddDate(0, 0, 2) //The result of newTime is time type //Get the current time after 2 monthsnewTime := ().AddDate(0, 2, 0)
This is the article about Go obtaining the specific implementation of the time difference between two time points. For more relevant Go obtaining time difference content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!