前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Golang之时间、日期类型

Golang之时间、日期类型

作者头像
超蛋lhy
发布2018-08-31 11:43:53
4K0
发布2018-08-31 11:43:53
举报
文章被收录于专栏:PythonistaPythonista

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

代码语言:javascript
复制
package main

import (
    "fmt"
    "time"
)

//获取时间的格式
func testTime() {
    now := time.Now()
    fmt.Printf("current time:%v\n", 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\n ", 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\n", timestamp)
    fmt.Printf("%02d-%02d-%02d %02d:%02d:%02d\n", year, month, day, hour, minute, second)
}

func processTask() {
    fmt.Printf("do task\n")
}

//定时器
func testTicker() {
    ticker := time.Tick(1 * time.Second)
    for i := range ticker {
        fmt.Printf("%v\n", i)
        processTask()
    }
}

//time.Duration用来表示纳秒
func testConst() {
    //一些常量
    fmt.Printf("nano second:%d\n", time.Nanosecond)
    fmt.Printf("micro second:%d\n", time.Microsecond)
    fmt.Printf("mili second:%d\n", time.Millisecond)
    fmt.Printf("second:%d\n", time.Second)
}
//时间格式化
func testFormat() {
    now := time.Now()
    timeStr := now.Format("2006-01-02 15:04:05")
    fmt.Printf("time:%s\n", timeStr)
}

func main() {
    //testTime()
    //timestamp := time.Now().Unix()
    //testTimestamp(timestamp)
    //testTicker()
    //testConst()
    testFormat()
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-02-09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档