time _ golang

本文展示了Go语言中对时间与持续时间进行的广泛支持,通过实例代码演示了如何获取当前时间、日期、时间戳、时区、时间比较、时间差计算以及时间转换等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Go's offers extensive support for times and durations; here are some example

package main

import (
    "fmt"
    "time"
)

func main() {

    p := fmt.Println

    now := time.Now()
    p(now)

    then := time.Date(
        2009, 11, 17, 20, 34, 58, 651387237, time.UTC)
    p(then)

    p(then.Year())
    p(then.Month())
    p(then.Day())
    p(then.Hour())
    p(then.Minute())
    p(then.Second())
    p(then.Nanosecond())
    p(then.Location())

    p(then.Before(now))
    p(then.After(now))
    p(then.Equal(now))

    diff := now.Sub(then)
    p(diff)

    p(diff.Hours())
    p(diff.Minutes())
    p(diff.Seconds())
    p(diff.Nanoseconds())

    p(then.Add(diff))
    p(then.Add(-diff))
}
2015-03-25 14:12:14.302895576 +0800 CST
2009-11-17 20:34:58.651387237 +0000 UTC
2009
November
17
20
34
58
651387237
UTC
true
false
false
46881h37m15.651508339s
46881.62101430787
2.8128972608584724e+06
1.6877383565150833e+08
168773835651508339
2015-03-25 06:12:14.302895576 +0000 UTC
2004-07-13 10:57:42.999878898 +0000 UTC

 

转载于:https://www.cnblogs.com/jackkiexu/p/4365569.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值