Interface Documentation:

https://pkg.go.dev/github.com/gogf/gf/v2/os/gtime

The methods are quite simple, with the more commonly used methods as follows:

  1. Timestamp is used to obtain the current timestamp, while TimestampMilli, TimestampMicro, and TimestampNano are used to get the current millisecond, microsecond, and nanosecond values.
  2. Date and Datetime are used to obtain the current date and current date-time.
  3. SetTimeZone is used to set the global timezone for the current process.
  4. For descriptions of other methods, please refer to the interface documentation.

Example 1: Basic Usage

Create a time object and get the current timestamp.

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/v2/os/gtime"
  5. )
  6. func main() {
  7. fmt.Println("Date :", gtime.Date())
  8. fmt.Println("Datetime :", gtime.Datetime())
  9. fmt.Println("Second :", gtime.Timestamp())
  10. fmt.Println("Millisecond:", gtime.TimestampMilli())
  11. fmt.Println("Microsecond:", gtime.TimestampMicro())
  12. fmt.Println("Nanosecond :", gtime.TimestampNano())
  13. }

After execution, the output is:

  1. Date : 2018-07-22
  2. Datetime : 2018-07-22 11:52:22
  3. Second : 1532231542
  4. Millisecond: 1532231542688
  5. Microsecond: 1532231542688688
  6. Nanosecond : 1532231542688690259

Example 2: StrToTime

In addition to using the New method, you can also use StrToTime to parse common time strings into a gtime.Time object. Common time strings include:

  1. 2017-12-14 04:51:34 +0805 LMT
  2. 2017-12-14 04:51:34 +0805 LMT
  3. 2006-01-02T15:04:05Z07:00
  4. 2014-01-17T01:19:15+08:00
  5. 2018-02-09T20:46:17.897Z
  6. 2018-02-09 20:46:17.897
  7. 2018-02-09T20:46:17Z
  8. 2018-02-09 20:46:17
  9. 2018/10/31 - 16:38:46
  10. 2018-02-09
  11. 2018.02.09
  12. 01-Nov-2018 11:50:28
  13. 01/Nov/2018 11:50:28
  14. 01.Nov.2018 11:50:28
  15. 01.Nov.2018:11:50:28
  16. Date connectors support '-', '/', '.'

Usage example:

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/v2/os/glog"
  5. "github.com/gogf/gf/v2/os/gtime"
  6. "time"
  7. )
  8. func main() {
  9. array := []string{
  10. "2017-12-14 04:51:34 +0805 LMT",
  11. "2006-01-02T15:04:05Z07:00",
  12. "2014-01-17T01:19:15+08:00",
  13. "2018-02-09T20:46:17.897Z",
  14. "2018-02-09 20:46:17.897",
  15. "2018-02-09T20:46:17Z",
  16. "2018-02-09 20:46:17",
  17. "2018.02.09 20:46:17",
  18. "2018-02-09",
  19. "2017/12/14 04:51:34 +0805 LMT",
  20. "2018/02/09 12:00:15",
  21. "01/Nov/2018:13:28:13 +0800",
  22. "01-Nov-2018 11:50:28 +0805 LMT",
  23. "01-Nov-2018T15:04:05Z07:00",
  24. "01-Nov-2018T01:19:15+08:00",
  25. "01-Nov-2018 11:50:28 +0805 LMT",
  26. "01/Nov/2018 11:50:28",
  27. "01/Nov/2018:11:50:28",
  28. "01.Nov.2018:11:50:28",
  29. "01/Nov/2018",
  30. }
  31. cstLocal, _ := time.LoadLocation("Asia/Shanghai")
  32. for _, s := range array {
  33. if t, err := gtime.StrToTime(s); err == nil {
  34. fmt.Println(s)
  35. fmt.Println(t.UTC().String())
  36. fmt.Println(t.In(cstLocal).String())
  37. } else {
  38. glog.Error(s, err)
  39. }
  40. fmt.Println()
  41. }
  42. }

In this example, some time strings are converted to gtime.Time objects using the StrToTime method, and both the UTC time and CST time (Shanghai timezone time) of the event are output. After execution, the output is:

  1. 2017-12-14 04:51:34 +0805 LMT
  2. 2017-12-13 20:46:34
  3. 2017-12-14 04:46:34 +0800 CST
  4. 2006-01-02T15:04:05Z07:00
  5. 2006-01-02 22:04:05
  6. 2006-01-03 06:04:05 +0800 CST
  7. 2014-01-17T01:19:15+08:00
  8. 2014-01-16 17:19:15
  9. 2014-01-17 01:19:15 +0800 CST
  10. 2018-02-09T20:46:17.897Z
  11. 2018-02-09 20:46:17
  12. 2018-02-10 04:46:17.897 +0800 CST
  13. 2018-02-09 20:46:17.897
  14. 2018-02-09 12:46:17
  15. 2018-02-09 20:46:17.897 +0800 CST
  16. 2018-02-09T20:46:17Z
  17. 2018-02-09 20:46:17
  18. 2018-02-10 04:46:17 +0800 CST
  19. 2018-02-09 20:46:17
  20. 2018-02-09 12:46:17
  21. 2018-02-09 20:46:17 +0800 CST
  22. 2018.02.09 20:46:17
  23. 2018-02-09 12:46:17
  24. 2018-02-09 20:46:17 +0800 CST
  25. 2018-02-09
  26. 2018-02-08 16:00:00
  27. 2018-02-09 00:00:00 +0800 CST
  28. 2017/12/14 04:51:34 +0805 LMT
  29. 2017-12-13 20:46:34
  30. 2017-12-14 04:46:34 +0800 CST
  31. 2018/02/09 12:00:15
  32. 2018-02-09 04:00:15
  33. 2018-02-09 12:00:15 +0800 CST
  34. 01/Nov/2018:13:28:13 +0800
  35. 2018-11-01 05:28:13
  36. 2018-11-01 13:28:13 +0800 CST
  37. 01-Nov-2018 11:50:28 +0805 LMT
  38. 2018-11-01 03:45:28
  39. 2018-11-01 11:45:28 +0800 CST
  40. 01-Nov-2018T15:04:05Z07:00
  41. 2018-11-01 22:04:05
  42. 2018-11-02 06:04:05 +0800 CST
  43. 01-Nov-2018T01:19:15+08:00
  44. 2018-10-31 17:19:15
  45. 2018-11-01 01:19:15 +0800 CST
  46. 01-Nov-2018 11:50:28 +0805 LMT
  47. 2018-11-01 03:45:28
  48. 2018-11-01 11:45:28 +0800 CST
  49. 01/Nov/2018 11:50:28
  50. 2018-11-01 03:50:28
  51. 2018-11-01 11:50:28 +0800 CST
  52. 01/Nov/2018:11:50:28
  53. 2018-11-01 03:50:28
  54. 2018-11-01 11:50:28 +0800 CST
  55. 01.Nov.2018:11:50:28
  56. 2018-11-01 03:50:28
  57. 2018-11-01 11:50:28 +0800 CST
  58. 01/Nov/2018
  59. 2018-10-31 16:00:00
  60. 2018-11-01 00:00:00 +0800 CST