zoukankan      html  css  js  c++  java
  • Go

    在编程中,程序员会经常使用到日期相关的函数,例如:统计某段代码执行花费的时间等等;
     
        1.时间和日期相关函数,都是在time包中;
                // 获取当前时间    
                now := time.Now()     
                // 输出结果:now=2020-11-09 18:29:17.9963558 +0800 CST m=+0.004987501 now type =time.Time                             
                fmt.Printf("now=%v now type =%T", now, now)
     
        2.// 通过now 来获取当前的年月日 时分秒    
            fmt.Printf("年=%v  ", now.Year())     
            fmt.Printf("月=%v  ", now.Month())  // 可以通过int 把英文强转为 int      
            fmt.Printf("月=%v  ", int(now.Month()))     
            fmt.Printf("日=%v  ", now.Day())     
            fmt.Printf("时=%v  ", now.Hour())     
            fmt.Printf("分=%v  ", now.Minute())     
            fmt.Printf("秒=%v  ", now.Second())
     
        3.// 也可以使用格式化输出    但是时间是固定的,格式可以调整
            fmt.Println(now.Format("2006-01-02 15:04:05"))     
            fmt.Println(now.Format("2006/01/02 15:04:05"))     
            fmt.Println(now.Format("2006-01-02"))     
            fmt.Println(now.Format("15:04:05"))
     
        4.时间常量结合休眠
            i := 0    
            for {         
                i ++         
                // time.Sleep(time.Second)  // 每隔1秒输出一个数字         
                time.Sleep(time.Millisecond * 100)  // 每隔0.1秒输出一个数字 即微妙 * 100 --> 0.1秒         
                fmt.Println(i)         
                if i > 100 {             
                    break         
                }     
            }
     
        5.时间戳Unix(秒数) / UnixNano(纳秒)
            fmt.Println("unix时间戳:", now.Unix(), "unixNano时间戳:", now.UnixNano())
     
     
  • 相关阅读:
    在operator =中要处理“自我赋值”
    delete指针以后应赋值为NULL
    【转】C++对成员访问运算符->的重载
    【转】浅析SkipList跳跃表原理及代码实现
    【KakaJSON手册】05_JSON转Model_05_动态模型
    【KakaJSON手册】04_JSON转Model_04_值过滤
    【KakaJSON手册】03_JSON转Model_03_key处理
    【KakaJSON手册】02_JSON转Model_02_数据类型
    【KakaJSON手册】01_JSON转Model_01_基本用法
    利用eclipse调试JDK源码
  • 原文地址:https://www.cnblogs.com/guo-s/p/13954125.html
Copyright © 2011-2022 走看看