go时间和日期相关函数

不念不忘少年蓝@ 2021-07-25 00:45 532阅读 0赞

一 点睛

在编程中,程序员会经常使用到日期相关的函数,比如:统计某段代码执行花费的时间等等。

1 时间和日期相关函数,需要导入 time 包。

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NoZW5ncWl1bWluZw_size_16_color_FFFFFF_t_70

2 time.Time 类型,用于表示时间

a 代码

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. func main() {
  7. // 获取当前时间
  8. now := time.Now()
  9. fmt.Printf("now=%v now type=%T\n", now, now)
  10. }

b 测试

  1. now=2021-04-14 18:08:39.6789462 +0800 CST m=+0.014000201 now type=time.Time

3 如何获取到其它的日期信息

a 代码

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. func main() {
  7. // 获取当前时间
  8. now := time.Now()
  9. // 通过now可以获取到年月日,时分秒
  10. fmt.Printf("年=%v\n", now.Year())
  11. fmt.Printf("月=%v\n", now.Month())
  12. fmt.Printf("月=%v\n", int(now.Month()))
  13. fmt.Printf("日=%v\n", now.Day())
  14. fmt.Printf("时=%v\n", now.Hour())
  15. fmt.Printf("分=%v\n", now.Minute())
  16. fmt.Printf("秒=%v\n", now.Second())
  17. }

b 测试

  1. 年=2021
  2. 月=April
  3. 月=4
  4. 日=14
  5. 时=18
  6. 分=10
  7. 秒=5

4 格式化日期时间

方式一: 就是使用 Printf 或者 SPrintf

a 代码

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. func main() {
  7. // 获取当前时间
  8. now := time.Now()
  9. // 格式化日期时间
  10. fmt.Printf("当前年月日 %d-%d-%d %d:%d:%d \n", now.Year(),
  11. now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second())
  12. dateStr := fmt.Sprintf("当前年月日 %d-%d-%d %d:%d:%d \n", now.Year(),
  13. now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second())
  14. fmt.Printf("dateStr=%v\n", dateStr)
  15. }

b 测试

  1. 当前年月日 2021-4-14 18:12:4
  2. dateStr=当前年月日 2021-4-14 18:12:4

方式二: 使用 time.Format() 方法完成

a 代码

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. func main() {
  7. // 获取当前时间
  8. now := time.Now()
  9. //格式化日期时间的第二种方式
  10. fmt.Printf(now.Format("2006-01-02 15:04:05"))
  11. fmt.Println()
  12. fmt.Printf(now.Format("2006-01-02"))
  13. fmt.Println()
  14. fmt.Printf(now.Format("15:04:05"))
  15. fmt.Println()
  16. fmt.Printf(now.Format("2006"))
  17. fmt.Println()
  18. }

b 测试

  1. 2021-04-14 18:14:00
  2. 2021-04-14
  3. 18:14:00
  4. 2021

c 说明

“2006/01/02 15:04:05” 这个字符串的各个数字是固定的,必须是这样写。

“2006/01/02 15:04:05” 这个字符串各个数字可以自由的组合,这样可以按程序需求来返回时间和日期。

5 时间的常量

  1. const (
  2. Nanosecond Duration = 1 // 纳秒
  3. Microsecond = 1000 * Nanosecond // 微秒
  4. Millisecond = 1000 * Microsecond // 毫秒
  5. Second = 1000 * Millisecond // 秒
  6. Minute = 60 * Second // 分钟
  7. Hour = 60 * Minute // 小时
  8. )

常量的作用——在程序中可用于获取指定时间单位的时间,比如想得到 100 毫秒。

  1. 100 * time. Millisecond

6 结合 Sleep 来使用一下时间常量

a 代码

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. func main() {
  7. // 需求,每隔1秒中打印一个数字,打印到10时就退出。
  8. // 需求2: 每隔0.1秒中打印一个数字,打印到10时就退出。
  9. i := 0
  10. for {
  11. i++
  12. fmt.Println(i)
  13. // 休眠
  14. // time.Sleep(time.Second)
  15. time.Sleep(time.Millisecond * 100)
  16. if i == 10 {
  17. break
  18. }
  19. }
  20. }

b 测试

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10

7 time 的 Unix 和 UnixNano 的方法

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NoZW5ncWl1bWluZw_size_16_color_FFFFFF_t_70 1

a 代码

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. func main() {
  7. // 获取当前时间
  8. now := time.Now()
  9. //Unix和UnixNano的使用
  10. fmt.Printf("unix时间戳=%v unixnano时间戳=%v\n", now.Unix(), now.UnixNano())
  11. }

b 测试

  1. unix时间戳=1618395648 unixnano时间戳=1618395648974226600

二 实战

1 需求

编写一段代码来统计 函数 test03 执行的时间。

2 代码

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. "strconv"
  6. )
  7. func test03() {
  8. str := ""
  9. for i := 0; i < 100000; i++ {
  10. str += "hello" + strconv.Itoa(i)
  11. }
  12. }
  13. func main() {
  14. // 在执行 test03 前,先获取到当前的 unix 时间戳
  15. start := time.Now().Unix()
  16. test03()
  17. end := time.Now().Unix()
  18. fmt.Printf("执行test03()耗费时间为%v秒\n", end-start)
  19. }

3 测试

  1. 执行test03()耗费时间为7

发表评论

表情:
评论列表 (有 0 条评论,532人围观)

还没有评论,来说两句吧...

相关阅读

    相关 MYSQL日期时间函数

    MYSQL日期和时间函数 大汇总这里是一个使用日期函数的例子。下面的查询选择了所有记录,其date\_col的值是在最后30天以内: [mysql][] SELECT some

    相关 日期时间函数

    在Oracle 11g中,系统提供了许多用于处理日期和时间的函数,通过这些函数可以实现计算需要的特定日期和时间,常用的日期和时间函数如表7.3所示。 表7.3常用日期时间类函