Golang Date and Time pack usage details
- 2020-07-21 08:23:15
- OfStack
golang date and time package: How time is used.
time package contains the time.Time time object and some methods for building this time object (time.Unix (), time.Parse ()) golang can be accurate to nanosecond, so the corresponding function return value or parameters are in nanoseconds, we can use time. ParseDuration(durationString string) friendly generation of nanosecond measurement time span value The time format string Layout is fixed at 2006-01-02 15:04:05 golang defaults to Local, the local time zone, which can be set by time.LoadLocation (zoneName string) (*Location, error)
Time zone build/format pattern string
// Build time zone
var timeLocation *time.Location
timeLocation, _ = time.LoadLocation("") //UTC
timeLocation, _ = time.LoadLocation("UTC") //UTC
timeLocation, _ = time.LoadLocation("Local") //Local
timeLocation, _ = time.LoadLocation("Asia/Shanghai") // Use time zone code
//golang Time formatting pattern
var timeLayout = "2006-01-02 15:04:05"
Current time object
// Gets the current time object
var timer time.Time
timer = time.Now()
// Set a time zone for your time Can be achieved by timer.Local()/timer.UTC() Fast time zone setting
timer.In(timeLocation)
Gets the second timestamp/nanosecond timestamp
// Gets the current second timestamp
var curTimestamp int64
curTimestamp = timer.Unix()
println("current timestamp:" + strconv.FormatInt(curTimestamp, 10))
// Gets the current nanosecond and timestamp 1 seconds =1000 ms =1000,000 subtle =1000,000,000 A nanosecond
var curNanoTimestamp int64
curNanoTimestamp = timer.UnixNano()
println("current nano timestamp:" + strconv.FormatInt(curNanoTimestamp, 10))
Gets the local time zone /CST standard time/custom format
// Gets the time zone for local time / Quickly get the time zone / Custom format
timeZone, _ := timer.Zone()
fmt.Printf("time zone: %s\n", timeZone)
fmt.Printf("time location: %s\n", timer.Location())
fmt.Printf("time in local zone: %s\n", timer.Local().String())
fmt.Printf("time in utc zone: %s\n", timer.UTC().String())
fmt.Printf("time: %s\n", timer.String())
fmt.Printf("time formatted: %s\n", timer.Format("2006-01-02 15:04:05"))
Gets the current year/month/day at: minutes: nanoseconds
// Get the current year / month / day when : points : seconds A nanosecond
fmt.Printf("current year: %d\n", timer.Year())
fmt.Printf("current month: %d %s\n", timer.Month(), timer.Month()) // The returned Month object
fmt.Printf("current day: %d\n", timer.Day())
fmt.Printf("current hour: %d\n", timer.Hour())
fmt.Printf("current minute: %d\n", timer.Minute())
fmt.Printf("current second: %d\n", timer.Second())
fmt.Printf("current nanosecond: %d\n", timer.Nanosecond())
Gets the current time/date
// Get current time / The date of
curHour, curMinute, curSecond := timer.Clock()
fmt.Printf("current clock: %d:%02d:%02d\n", curHour, curMinute, curSecond)
curYear, curMonth, curDay := timer.Date()
fmt.Printf("current date: %d-%02d-%02d\n", curYear, curMonth, curDay)
Edit time/Find the difference between two dates
time.ParseDuration(durationString string) makes it convenient for us to use semantics to construct the time span value, which is in nanoseconds, such as:
timeDuration, _ := time.ParseDuration("24h")
timeDuration, _ := time.ParseDuration("12m")
timeDuration, _ := time.ParseDuration("6s")
timeDuration, _ := time.ParseDuration("1ms")
timeDuration, _ := time.ParseDuration("1us")
timeDuration, _ := time.ParseDuration("1ns")
// Has the current time as the base year of growth / month / Future time objects
timerAdded := timer.AddDate(1, 2, 3)
curYear, curMonth, curDay = timerAdded.Date()
fmt.Printf("current date: %d-%02d-%02d\n", curYear, curMonth, curDay)
// Growth based on current time N Time objects after nanoseconds Like growth 1 day
timeDuration, _ := time.ParseDuration("24h")
timerAdded = timer.Add(timeDuration)
// Calculate the difference between the two times It returns nanoseconds Other units shall be calculated by themselves as required
// Duration is type of int64 and nanoseconds
timeDuration = timerAdded.Sub(timer)
fmt.Printf("days duration between %s~%s: %d\n",
timerAdded.Format(timeLayout),
timer.Format(timeLayout),
timeDuration/1000/1000/1000/24/60/60)
Build the time object using the time string/Unix Timestamp
// Get a time object using a time string
timer, _ = time.Parse(timeLayout, "2018-08-08 08:08:08")
// Get a time object using a time string And set the time zone
timer, _ = time.ParseInLocation(timeLayout, "2018-08-08 08:08:08", timeLocation)
// use Unix Timestamp builds the time object
timer = time.Unix(1552368806, 0) //2019-03-12 13:33:26 the Unix The time stamp
fmt.Println(timer.Format(timeLayout))
The current time is the day of the year and the day of the week
Note that Weekday on Sunday is numbered 0
// The current time is the day of the year Which day of the week
fmt.Printf("year day: %d, week day: %d\n", timer.YearDay(), timer.Weekday())
Use the representation string conversion time span
// Use the representation string conversion time span
timeDuration, _ = time.ParseDuration("300s")
fmt.Printf("nanosecond: %d\n", timeDuration)
timeDuration, _ = time.ParseDuration("300us")
fmt.Printf("nanosecond: %d\n", timeDuration)