• Golang之时间、日期类型


    孤身只影的一直小地鼠,艰难的走在路上

    package main
    
    import (
        "fmt"
        "time"
    )
    
    //获取时间的格式
    func testTime() {
        now := time.Now()
        fmt.Printf("current time:%v
    ", now)
    
        year := now.Year()
        month := now.Month()
        day := now.Day()
        hour := now.Hour()
        minute := now.Minute()
        second := now.Second()
        // %02d表示不够2位的话,就补0
        fmt.Printf("%02d-%02d-%02d %02d:%02d:%02d
     ", year, month, day, hour, minute, second)
    }
    
    //获取时间戳
    func testTimestamp(timestamp int64) {
        timeObj := time.Unix(timestamp, 0)
        year := timeObj.Year()
        month := timeObj.Month()
        day := timeObj.Day()
        hour := timeObj.Hour()
        minute := timeObj.Minute()
        second := timeObj.Second()
    
        fmt.Printf("current timestamp:%d
    ", timestamp)
        fmt.Printf("%02d-%02d-%02d %02d:%02d:%02d
    ", year, month, day, hour, minute, second)
    }
    
    func processTask() {
        fmt.Printf("do task
    ")
    }
    
    //定时器
    func testTicker() {
        ticker := time.Tick(1 * time.Second)
        for i := range ticker {
            fmt.Printf("%v
    ", i)
            processTask()
        }
    }
    
    //time.Duration用来表示纳秒
    func testConst() {
        //一些常量
        fmt.Printf("nano second:%d
    ", time.Nanosecond)
        fmt.Printf("micro second:%d
    ", time.Microsecond)
        fmt.Printf("mili second:%d
    ", time.Millisecond)
        fmt.Printf("second:%d
    ", time.Second)
    }
    //时间格式化
    func testFormat() {
        now := time.Now()
        timeStr := now.Format("2006-01-02 15:04:05")
        fmt.Printf("time:%s
    ", timeStr)
    }
    
    func main() {
        //testTime()
        //timestamp := time.Now().Unix()
        //testTimestamp(timestamp)
        //testTicker()
        //testConst()
        testFormat()
    }
  • 相关阅读:
    bootstrap里的下拉菜单
    bootstrap里的图标
    bootstrap
    maven和svn区别
    maven
    计算机程序的思维逻辑 (20)
    计算机程序的思维逻辑 (19)
    计算机程序的思维逻辑 (18)
    计算机程序的思维逻辑 (17)
    计算机程序的思维逻辑 (15)
  • 原文地址:https://www.cnblogs.com/pyyu/p/8435535.html
Copyright © 2020-2023  润新知