ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

golang的time包:时间字符串和时间戳的相互转换

2021-09-21 11:03:51  阅读:125  来源: 互联网

标签:02 00 01 fmt golang Println time 字符串


本博客转自:https://www.cnblogs.com/nyist-xsk/p/11448348.html

package main

import (
    "log"
    "time"
)

func main() {
    t := int64(1546926630)      //外部传入的时间戳(秒为单位),必须为int64类型
    t1 := "2019-01-08 13:50:30" //外部传入的时间字符串

    //时间转换的模板,golang里面只能是 "2006-01-02 15:04:05" (go的诞生时间)
    timeTemplate1 := "2006-01-02 15:04:05" //常规类型
    timeTemplate2 := "2006/01/02 15:04:05" //其他类型
    timeTemplate3 := "2006-01-02"          //其他类型
    timeTemplate4 := "15:04:05"            //其他类型

    // ======= 将时间戳格式化为日期字符串 =======
    log.Println(time.Unix(t, 0).Format(timeTemplate1)) //输出:2019-01-08 13:50:30
    log.Println(time.Unix(t, 0).Format(timeTemplate2)) //输出:2019/01/08 13:50:30
    log.Println(time.Unix(t, 0).Format(timeTemplate3)) //输出:2019-01-08
    log.Println(time.Unix(t, 0).Format(timeTemplate4)) //输出:13:50:30

    // ======= 将时间字符串转换为时间戳 =======
    stamp, _ := time.ParseInLocation(timeTemplate1, t1, time.Local) //使用parseInLocation将字符串格式化返回本地时区时间
    log.Println(stamp.Unix())  //输出:1546926630
}

package main

import (
    "log"
    "time"
)
func main(){
    // 获取每天的零点时间戳, 一个小时的时间戳是3600
    timeStr := time.Now().Format("2006-01-02")
    t, _ := time.ParseInLocation("2006-01-02", timeStr, time.Local)
    timeUnix := t.Unix()
}
package main

import (
    "fmt"
    "strconv"
    "time"
)

func main() {
    t := time.Now()
    fmt.Println(t)

    fmt.Println(t.UTC().Format(time.UnixDate))

    fmt.Println(t.Unix())

    timestamp := strconv.FormatInt(t.UTC().UnixNano(), 10)
    fmt.Println(timestamp)
    timestamp = timestamp[:10]
    fmt.Println(timestamp)
}

//输出: 
//2019-09-02 19:17:58.2508394 +0800 CST m=+0.001994001
//Mon Sep  2 11:17:58 UTC 2019
//1567423078
//1567423078250839400
//1567423078
package main

import (
    "fmt"
    "strconv"
    "time"
)

func main() {
    const longForm = "Jan 2, 2006 at 3:04pm (MST)"
    t, _ := time.Parse(longForm, "Jun 21, 2017 at 0:00am (PST)")
    fmt.Println(t)

    const shortForm = "2006-Jan-02"
    t, _ = time.Parse(shortForm, "2017-Jun-21")  // 字符串转时间
    fmt.Println(t)

    t, _ = time.Parse("01/02/2006", "06/21/2017") // 字符串转时间
    fmt.Println(t)
    fmt.Println(t.Unix())

    i, err := strconv.ParseInt("1498003200", 10, 64)
    if err != nil {
        panic(err)
    }
    tm := time.Unix(i, 0)
    fmt.Println(tm)

    var timestamp int64 = 1498003200
    tm2 := time.Unix(timestamp, 0)
    fmt.Println(tm2.Format("2006-01-02 03:04:05 PM"))
    fmt.Println(tm2.Format("02/01/2006 15:04:05 PM"))
}

//输出
//2017-06-21 00:00:00 +0000 PST
//2017-06-21 00:00:00 +0000 UTC
//2017-06-21 00:00:00 +0000 UTC
//1498003200
//2017-06-21 08:00:00 +0800 CST
//2017-06-21 08:00:00 AM
//21/06/2017 08:00:00 AM

//time常用方法

After(u Time) bool 
时间类型比较,是否在Time之后

Before(u Time) bool 
时间类型比较,是否在Time之前

Equal(u Time) bool 
比较两个时间是否相等

IsZero() bool 
判断时间是否为零值,如果sec和nsec两个属性都是0的话,则该时间类型为0

Date() (year int, month Month, day int) 
返回年月日,三个参数

Year() int 
返回年份

Month() Month 
返回月份.是Month类型

Day() int 
返回多少号

Weekday() Weekday 
返回星期几,是Weekday类型

ISOWeek() (year, week int) 
返回年份,和该填是在这年的第几周.

Clock() (hour, min, sec int) 
返回小时,分钟,秒

Hour() int 
返回小时

Minute() int 
返回分钟

Second() int 
返回秒数

Nanosecond() int 
返回纳秒
package main

import (
    "fmt"
    "strings"
    "time"
)

func main() {
    // Add 时间相加
    now := time.Now()
    // ParseDuration parses a duration string.
    // A duration string is a possibly signed sequence of decimal numbers,
    // each with optional fraction and a unit suffix,
    // such as "300ms", "-1.5h" or "2h45m".
    //  Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
    // 10分钟前
    m, _ := time.ParseDuration("-1m")
    m1 := now.Add(m)
    fmt.Println(m1)

    // 8个小时前
    h, _ := time.ParseDuration("-1h")
    h1 := now.Add(8 * h)
    fmt.Println(h1)

    // 一天前
    d, _ := time.ParseDuration("-24h")
    d1 := now.Add(d)
    fmt.Println(d1)

    printSplit(50)

    // 10分钟后
    mm, _ := time.ParseDuration("1m")
    mm1 := now.Add(mm)
    fmt.Println(mm1)

    // 8小时后
    hh, _ := time.ParseDuration("1h")
    hh1 := now.Add(hh)
    fmt.Println(hh1)

    // 一天后
    dd, _ := time.ParseDuration("24h")
    dd1 := now.Add(dd)
    fmt.Println(dd1)

    printSplit(50)

    // Sub 计算两个时间差
    subM := now.Sub(m1)
    fmt.Println(subM.Minutes(), "分钟")

    sumH := now.Sub(h1)
    fmt.Println(sumH.Hours(), "小时")

    sumD := now.Sub(d1)
    fmt.Printf("%v 天\n", sumD.Hours()/24)

}

func printSplit(count int) {
    fmt.Println(strings.Repeat("#", count))
}

 

标签:02,00,01,fmt,golang,Println,time,字符串
来源: https://www.cnblogs.com/tortoise512/p/15316156.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有