golang's date and time package: how to use time.
- The time package contains the time object and some methods to build this time object ((), ())
- golang can be accurate to nanosecond, so the corresponding function return value or parameter is in nanosecond units. We can use the (durationString string) to friendly generate the time span value of the nanosecond metric.
- The time format string Layout of golang is fixed to 2006-01-02 15:04:05
- golang uses Local, which is the local time zone by default. You can set the time zone through (zoneName string) (*Location, error).
Time zone build/format pattern string
// Build time zonevar timeLocation * timeLocation, _ = ("") //UTC timeLocation, _ = ("UTC") //UTC timeLocation, _ = ("Local") //Local timeLocation, _ = ("Asia/Shanghai") //Use time zone code //Golang's time format patternvar timeLayout = "2006-01-02 15:04:05"
Current time object
// Get the current time objectvar timer timer = () // Set time zone for time You can quickly set time zone through ()/()(timeLocation)
Get seconds time stamp/nanosecond time stamp
// Get the current second-level time stampvar curTimestamp int64 curTimestamp = () println("current timestamp:" + (curTimestamp, 10)) // Get the current nanosecond and time stamp 1 second = 1000 millisecond = 1000,000 subtle = 1000,000,000 nanosecondsvar curNanoTimestamp int64 curNanoTimestamp = () println("current nano timestamp:" + (curNanoTimestamp, 10))
Get the time zone/CST standard time/custom format for local time
// Get the time zone of local time/quickly get the time zone time/custom formattimeZone, _ := () ("time zone: %s\n", timeZone) ("time location: %s\n", ()) ("time in local zone: %s\n", ().String()) ("time in utc zone: %s\n", ().String()) ("time: %s\n", ()) ("time formatted: %s\n", ("2006-01-02 15:04:05"))
Get the current year/month/day hour: minute: seconds nanoseconds
// Get the current year/month/day hour: minute: seconds nanoseconds("current year: %d\n", ()) ("current month: %d %s\n", (), ()) //Return Month object("current day: %d\n", ()) ("current hour: %d\n", ()) ("current minute: %d\n", ()) ("current second: %d\n", ()) ("current nanosecond: %d\n", ())
Get the current time/date
// Get the current time/datecurHour, curMinute, curSecond := () ("current clock: %d:%02d:%02d\n", curHour, curMinute, curSecond) curYear, curMonth, curDay := () ("current date: %d-%02d-%02d\n", curYear, curMonth, curDay)
Edit time/find the time difference between two dates
(durationString string) can facilitate us to use semantics to build time span values, with the numerical units in nanoseconds, for example:
timeDuration, _ := ("24h")
timeDuration, _ := ("12m")
timeDuration, _ := ("6s")
timeDuration, _ := ("1ms")
timeDuration, _ := ("1us")
timeDuration, _ := ("1ns")
// The current time is the time object after the year/month/day growthtimerAdded := (1, 2, 3) curYear, curMonth, curDay = () ("current date: %d-%02d-%02d\n", curYear, curMonth, curDay) // The time object after N nanoseconds is increased based on the current time, for example, one day has increasedtimeDuration, _ := ("24h") timerAdded = (timeDuration) // Calculate the difference between two times. The returned is nanoseconds. Calculate other units by yourself according to the requirements.// Duration is type of int64 and nanoseconds timeDuration = (timer) ("days duration between %s~%s: %d\n", (timeLayout), (timeLayout), timeDuration/1000/1000/1000/24/60/60)
Build time objects using time strings / Unix Timestamp
// Use time string to get the time objecttimer, _ = (timeLayout, "2018-08-08 08:08:08") // Use the time string to get the time object and set the time zonetimer, _ = (timeLayout, "2018-08-08 08:08:08", timeLocation) // Build time objects using Unix timestamptimer = (1552368806, 0) //2019-03-12 Unix timestamp at 13:33:26((timeLayout))
Get what day of the current year and what day of the week
Note that the Weekday number for Sunday is 0
// Get what day of the current year and what day of the week("year day: %d, week day: %d\n", (), ())
Convert time span using characterization string
// Convert time span using characterization stringtimeDuration, _ = ("300s") ("nanosecond: %d\n", timeDuration) timeDuration, _ = ("300us") ("nanosecond: %d\n", timeDuration)
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.