• 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
    }
    
    
    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 
    ", 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
    ", 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
    ", year, month, day, hour, minute, second)
    
    }
    
    //时间戳
    func timestampDemo() {
    	now := time.Now()                                 //获取当前时间
    	timestamp1 := now.Unix()                          //时间戳
    	timestamp2 := now.UnixNano()                      //纳秒时间戳
    	fmt.Printf("current timestamp1:%v
    ", timestamp1) //current timestamp1:1585148417
    	fmt.Printf("current timestamp2:%v
    ", 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
    ", 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 
    ", 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
    ", 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
    ", 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
    ", 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
    ", 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
    ", 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/

  • 相关阅读:
    【zZ】OpenCV HOGDescriptor 参数图解
    [C]遍历目录下所有文件
    drawing
    转:基于用户投票的排名算法系列
    编码格式
    泛型
    接口
    隐藏方法不能实现多态性
    结构
    静态
  • 原文地址:https://www.cnblogs.com/haima/p/12171798.html
Copyright © 2020-2023  润新知