美文网首页
go 时间和日期函数

go 时间和日期函数

作者: StevenQin | 来源:发表于2019-03-02 13:59 被阅读0次

1、时间和日期相关函数,需要引入 time包。
2、time.Time类型,用来表示时间。

    //获取当前时间
    now := time.Now()
    fmt.Printf("now=%v,now的类型是%T\n", now, now)

3、获取当前时间方法 now := time.Now()
4、获取其它日期信息

//获取当前时间
    now := time.Now()
    fmt.Printf("now=%v,now的类型是%T\n", now, now)
    
    //now返回的是个结构体struct,里面有很多方法,类似于对象
    //通过now可以获得年月日,时分秒
    fmt.Printf("年=%v\n", now.Year())
    fmt.Printf("月=%v\n", now.Month())
    fmt.Printf("月=%v\n", int(now.Month()))
    fmt.Printf("日=%v\n", now.Day())
    fmt.Printf("时=%v\n", now.Hour())
    fmt.Printf("分=%v\n", now.Minute())
    fmt.Printf("秒=%v\n", now.Second())

5、格式化日期和时间

  • 方式1 使用Printf或者SPrintf
    //获取当前时间
    now := time.Now()
    fmt.Printf("now=%v,now的类型是%T\n", now, now)
    //格式化时间
    fmt.Printf("当前日期是:%02d-%02d-%02d %02d:%02d:%02d \n", now.Year(),
        now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second())
    //Sprintf 转成字符串的形式
    dateStr := fmt.Sprintf("当前日期是:%02d-%02d-%02d %02d:%02d:%02d \n", now.Year(),
        now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second())
    fmt.Printf("dateStr=%v\n", dateStr)
  • 方式2 使用 time.Format()方法。 2006 01 02 15 04 05是固定写法不能改变
    //获取当前时间
    now := time.Now()
    fmt.Printf("now=%v,now的类型是%T\n", now, now)
    //格式化时间
    fmt.Printf(now.Format("2006-01-02 15:04:05"))
    fmt.Println()
    fmt.Printf(now.Format("2006-01-02"))
    fmt.Println()
    fmt.Printf(now.Format("15:04:05"))
    fmt.Println()
    fmt.Printf(now.Format("15:04"))
    fmt.Println()

6、时间的常量

//常量定义 //见文档
const (
    Nanosecond  Duration = 1
    Microsecond          = 1000 * Nanosecond
    Millisecond          = 1000 * Microsecond
    Second               = 1000 * Millisecond
    Minute               = 60 * Second
    Hour                 = 60 * Minute
)

常量的作用是:用来获取指定时间单位的时间,比如:100毫秒写法是 100* time.Millisecond

7、休眠 (结合sleep来使用时间常量)

需求:每隔0.1秒,打印一个数字,到100时退出

package main

import (
    "fmt"
    _ "strconv"
    _ "strings"
    "time"
)

func main() {
    //需求:每隔0.1秒,打印一个数字,到100时退出
    i := 0
    for {
        i++
        fmt.Println(i)
        time.Sleep(time.Millisecond * 100)
        if i == 100 {
            break
        }
    }
}

8、获取当前Unix时间戳和Unixnano时间戳(作用是:获取随机的数字)

    now := time.Now()
    fmt.Printf("unix时间戳是:%v, unixnano时间戳是:%v \n", now.Unix(), now.UnixNano())
  • 最佳实践

计算test03函数执行的时间

package main

import (
    "fmt"
    "strconv"
    _ "strings"
    "time"
)

func test03() {
    str := ""
    for i := 0; i < 100000; i++ {
        str += "hello" + strconv.Itoa(i) //当i转为字符串
    }
}
func main() {
    //执行前先获取unix时间戳
    start := time.Now().Unix()
    test03()
    end := time.Now().Unix()
    fmt.Printf("用时为%v秒。\n", end-start)
}

相关文章

  • go 时间和日期函数

    1、时间和日期相关函数,需要引入 time包。2、time.Time类型,用来表示时间。 3、获取当前时间方法 n...

  • go-day3

    go-day3 outline strings 和 strconv 使用 Go中的时间和日期类型 流程控制 函数详...

  • 日期和时间函数

    获得当前日期的函数和获得当前时间的函数 curdate()和current_date()作用相同:输出当前日期cu...

  • mysql时间函数和日期函数

    转载自:时间函数

  • SQLite 日期 & 时间

    SQLite 日期 & 时间 SQLite 支持以下五个日期和时间函数: 上述五个日期和时间函数把时间字符串作为参...

  • 返回当前的年月日时间,就用这4个函数

    返回当前的日期和时间、进行日期格式转换、填写制表时间。就用日期时间函数。 1.使用NOW函数返回当前日期和时间的序...

  • PHP 日期和时间函数

    PHP date() 函数用于对日期或时间进行格式化。 PHP Date() 函数:函数把时间戳格式化为更易读的日...

  • MySQL日期和时间函数

    假设日期为 x = 20170427, y = '20170427', z = '2017-04-27' 三种格式...

  • MySQL日期和时间函数

    # MySQL日期和时间函数 - ADDDATE(EXPR, DAYS) 含义:向日期加上指定的天数 示例: SE...

  • Go语言获取时间和日期

    如何获取当前时间time.now() 如何获取当前的年月日时分秒now.年月日时分秒 如何按照我们指定的格式生成时...

网友评论

      本文标题:go 时间和日期函数

      本文链接:https://www.haomeiwen.com/subject/jnmeuqtx.html