Go語言基礎之time包


package main

import (
    "fmt"
    "time"
)

func main() {

    t := time.Now()   //2019-07-31 13:55:21.3410012 +0800 CST m=+0.006015601
    fmt.Println(t.Format("20060102150405"))

    //當前時間戳
    t1 := time.Now().Unix()  //1564552562
    fmt.Println(t1)
    //時間戳轉化為具體時間
    fmt.Println(time.Unix(t1, 0).String()) //2020-03-26 00:04:37 +0800 CST

    //基本格式化的時間表示
    fmt.Println(time.Now().String()) //2019-07-31 13:56:35.7766729 +0800 CST m=+0.005042501

    fmt.Println(time.Now().Format("2006-01-02"))  //2019-07-31
    fmt.Println(time.Now().Format("2006-01-02 15:04:05"))  //2019-07-31 13:57:52

    //獲取第幾周
    _, week := time.Now().ISOWeek()  
    //獲取年、月、日
    year, month, day := rwTools.DateYmdInts() 
}

// 時間戳轉年月日 時分秒
func DateFormat(timestamp int) string {
    tm := time.Unix(int64(timestamp), 0)
    return tm.Format("2006-01-02 15:04")
}

//時間戳轉年月日
func DateFormatYmd(timestamp int) string {
    tm := time.Unix(int64(timestamp), 0)
    return tm.Format("2006-01-02")
}

//獲取當前年月
func DateYmFormat() string {
    tm := time.Now()
    return tm.Format("2006-01")
}

//獲取年月日時分秒(字符串類型)
func DateNowFormatStr() string {
    tm := time.Now()
    return tm.Format("2006-01-02 15:04:05")
}

//時間戳
func DateUnix() int {
    t := time.Now().Local().Unix()
    return int(t)
}

//獲取年月日時分秒(time類型)
func DateNowFormat() time.Time {
    tm := time.Now()
    return tm
}

//獲取第幾周
func DateWeek() int {
    _, week := time.Now().ISOWeek()
    return week
}

//獲取年、月、日
func DateYMD() (int,int, int) {
    year, month, day := DateYmdInts()
    return year,month,day
}

// 獲取年月日
func DateYmdFormat() string {
    tm := time.Now()
    return tm.Format("2006-01-02")
}

// 獲取日期的年月日
func DateYmdInts() (int, int, int) {
    timeNow := time.Now()
    year, month, day := timeNow.Date()
    return year, int(month), day
}


//獲取當天的時間范偉 Time類型 2020-08-18 00:00:00 +0800 CST 2020-08-18 23:59:59 +0800 CST
func GetCurrentTimestamp() (beginTime, endTime time.Time) {
 t := time.Now()
 timeStr := t.Format("2006-01-02")
 beginTime, _ = time.ParseInLocation("2006-01-02", timeStr, time.Local)
 endTimeTmp := beginTime.Unix() + 86399
 endTime = time.Unix(endTimeTmp, 0)
 return beginTime, endTime
}
//獲取當天的時間范偉 int64類型 //1597680000 1597766399
func GetCurrentTimeInt64() (beginTime, endTime int64) {
 t := time.Now()
 timeStr := t.Format("2006-01-02")
 beginTimeTmp, _ := time.ParseInLocation("2006-01-02", timeStr, time.Local)
 beginTime = beginTimeTmp.Unix()
 endTime = beginTime + 86399
 return beginTime, endTime
}

package main

import (
	"fmt"
	"time"
)

func main() {

	sleepDemo()
}
func test1() {

	t := time.Now() //	獲取當前的時間對象
	//t type:time.Time value:time.Time{wall:0xbf96f99387cfb7c3, ext:190223, loc:(*time.Location)(0x571b80)}
	fmt.Printf("t type:%T value:%#v \n", t, t)

	//比較時間戳
	dataNumber1 := int64(1586252136)
	// dataNumber1 := endTime22
	if t.Unix() > dataNumber1 {
		fmt.Println("111111")
	}
	fmt.Println("22222222")

}

//時間類型
func timeDemo() {
	now := time.Now()

	fmt.Println("now time is ", now) //	now time is  2020-03-25 22:56:17.263142275 +0800 CST m=+0.000158437

	year := now.Year()
	fmt.Println("year:", year) //year: 2020

	month := now.Month()
	fmt.Printf("month:%02d\n", month) //month:03

	day := now.Day()
	fmt.Println("day:", day) //day: 25

	hour := now.Hour()
	fmt.Println("hour:", hour) //hour: 22

	minute := now.Minute()
	fmt.Println("minute:", minute) //minute: 56

	second := now.Second()
	fmt.Println("second:", second) //	second: 17

	// 2020-03-25 22:56:17
	fmt.Printf("%d-%02d-%02d %02d:%02d:%02d\n", year, month, day, hour, minute, second)

}

//時間戳
func timestampDemo() {
	now := time.Now()                                 //獲取當前時間
	timestamp1 := now.Unix()                          //時間戳
	timestamp2 := now.UnixNano()                      //納秒時間戳
	fmt.Printf("current timestamp1:%v\n", timestamp1) //current timestamp1:1585148417
	fmt.Printf("current timestamp2:%v\n", timestamp2) //current timestamp2:1585148417603894248
}

//使用time.Unix()函數可以將時間戳轉為時間格式。
//傳入時間戳 如:1586252136
func timestampDemo2(timestamp int64) {
	timeObj := time.Unix(timestamp, 0)                                                  //將時間戳轉為時間格式
	fmt.Println(timeObj)                                                                //2020-04-07 17:35:36 +0800 CST
	year := timeObj.Year()                                                              //年
	month := timeObj.Month()                                                            //月
	day := timeObj.Day()                                                                //日
	hour := timeObj.Hour()                                                              //小時
	minute := timeObj.Minute()                                                          //分鍾
	second := timeObj.Second()                                                          //秒
	fmt.Printf("%d-%02d-%02d %02d:%02d:%02d\n", year, month, day, hour, minute, second) //2020-04-07 17:35:36
}

//添加時間
func addTimeDemo() {
	t := time.Now()
	//一個月后的時間
	mealsNumber := 30
	endTime22 := t.Add(time.Hour * 24 * time.Duration(mealsNumber)).Unix()
	fmt.Printf("endTime22 type:%T value:%#v \n", endTime22, endTime22) //endTime22 type:int64 value:1587741038
}

// 時間格式化
func formatDemo() {
	now := time.Now()
	// 格式化的模板為Go的出生時間2006年1月2號15點04分 Mon Jan
	// 24小時制
	timeObj1 := now.Format("2006-01-02 15:04:05.000 Mon Jan")
	fmt.Println(timeObj1)                                 //2020-03-25 23:31:53.076 Wed Mar
	fmt.Printf("Type:%T value:%#v\n", timeObj1, timeObj1) //Type:string value:"2020-03-25 23:31:53.076 Wed Mar"

	// 12小時制
	fmt.Println(now.Format("2006-01-02 03:04:05.000 PM Mon Jan")) //2020-03-25 11:31:53.076 PM Wed Mar
	fmt.Println(now.Format("2006/01/02 15:04"))                   //2020/03/25 23:31
	fmt.Println(now.Format("15:04 2006/01/02"))                   //23:31 2020/03/25
	fmt.Println(now.Format("2006/01/02"))                         //2020/03/25
	fmt.Println(now.Format("20060102150405000"))                  //0200325233153000

}

//客符串時間格式化成時間類型
func stringToTime() {
	timestr := "2020-02-25 20:18:19"
	timeObj, err := time.Parse("2006-01-02 15:04:05", timestr)
	if err != nil {
		fmt.Printf("parse time failed , err:%v\n", err)
		return
	}
	fmt.Println(timeObj) //2020-02-25 20:18:19 +0000 UTC

	// Type:time.Time value:time.Time{wall:0x0, ext:63718258699, loc:(*time.Location)(nil)}
	fmt.Printf("Type:%T value:%#v\n", timeObj, timeObj)
	fmt.Println(timeObj.Unix()) //1582661899

	timestr1 := "2020-02-25"
	timeObj, err = time.Parse("2006-01-02", timestr1)
	if err != nil {
		fmt.Printf("parse time failed , err:%v\n", err)
		return
	}
	fmt.Println(timeObj) //2020-02-25 00:00:00 +0000 UTC

	// Type:time.Time value:time.Time{wall:0x0, ext:63718185600, loc:(*time.Location)(nil)}
	fmt.Printf("Type:%T value:%#v\n", timeObj, timeObj)
	fmt.Println(timeObj.Unix()) //1582588800

}

//睡5秒鍾
func sleepDemo() {
	fmt.Println("開始了...")
	n := 5 //int
	time.Sleep(time.Duration(n) * time.Second)
	// time.Sleep(5 * time.Second)
	fmt.Println("5秒鍾過去了...")
}

//Time類型.Unix  是將時間戳轉為Time類型
func test() {
    timestamp := time.Now().Unix() //time.Now()是當前時間戳
    fmt.Println("now",timestamp)  //now 1550377621

    #將時間戳轉為Time類型
    fmt.Println(time.Unix(timestamp, 0))  //2019-02-17 12:27:01 +0800 CST  
}

參考原文:
https://www.liwenzhou.com/posts/Go/go_time/


免責聲明!

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



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