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())
     
     
  • 相关阅读:
    VS2010版快捷键
    Win7旗舰版中的IIS配置asp.net的运行环境
    实现软件自动在线升级的原理
    view_countInfo
    C#尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
    error: 40
    SQL Server 2008 阻止保存要求重新创建表的更改问题的设置方法
    继承实现圆柱体面积体积的计算
    圆柱模板价格计算器V1.0版本
    python3.7内置函数整理笔记
  • 原文地址:https://www.cnblogs.com/guo-s/p/13954125.html
Copyright © 2011-2022 走看看