golang的time包:時間字符串和時間戳的相互轉換


本博客轉自: https://blog.csdn.net/mirage003/article/details/86073046

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))
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM