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())
     
     
  • 相关阅读:
    UVA 10618 Tango Tango Insurrection
    UVA 10118 Free Candies
    HDU 1024 Max Sum Plus Plus
    POJ 1984 Navigation Nightmare
    CODEVS 3546 矩阵链乘法
    UVA 1625 Color Length
    UVA 1347 Tour
    UVA 437 The Tower of Babylon
    UVA 1622 Robot
    UVA127-"Accordian" Patience(模拟)
  • 原文地址:https://www.cnblogs.com/guo-s/p/13954125.html
Copyright © 2011-2022 走看看