设为首页 加入收藏

TOP

Go基础编程实践(三)—— 日期和时间
2019-07-02 14:12:12 】 浏览:73
Tags:基础 编程 实践 日期 时间

日期和时间

package main

import (
    "fmt"
    "time"
)

func main() {
    // 获取当前时间
    current := time.Now()
    // 格式化字符串输出
    fmt.Println(current.String())

    // Format函数格式化输出
    // 无论要格式化什么时间,"2006-01-02 15:04:05"这几个数字固定不变
    fmt.Println("MM-DD-YYYY: ", current.Format("01-02-2006"))
    fmt.Println("hh:mm:ss MM-DD-YYYY:", current.Format("15:04:05 01-02-2006"))
    fmt.Println("YYYY-DD-MM hh:mm:ss:", current.Format("2006-01-02 15:04:05"))

    // 添加日期
    // AddDate的三个参数依次为年、月、日
    currentAddDate := current.AddDate(-1, 1, 0)
    fmt.Println(currentAddDate)

    // 添加时间
    currentAdd := current.Add(10 * time.Minute)
    fmt.Println(currentAdd)

    // 获取时间差,利用Sub函数或者利用Add/AddDate函数增加负值
    // Date函数参数:年-月-日-时-分-秒-纳秒
    currentSub := current.Sub(time.Date(2000, 1, 1, 1, 1, 1, 0, time.UTC))
    // currentSub :=currentAdd.Sub(current)
    fmt.Println(currentSub)

    // 从字符串解析时间
    str := "2019-06-29T17:17:17.777Z"
    layout := "2006-01-02T15:04:05.000Z"
    t, err := time.Parse(layout, str)
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(t)

}
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇在 Windows 中编译 Github 中的 G.. 下一篇在 Ubuntu 开启 GO 程序编译之旅

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目