美文网首页
Golang时间戳

Golang时间戳

作者: Feng_Sir | 来源:发表于2017-11-30 14:59 被阅读0次
    
    package main
    
    import (
      "fmt"
     "time"
    )
    func main() {
        //获取时间戳
        timestamp := time.Now().Unix()
    
        fmt.Println(timestamp)
    
        //格式化为字符串,tm为Time类型
    
        tm := time.Unix(timestamp, 0)
    
        fmt.Println(tm.Format("2006-01-02 03:04:05 PM"))
    
        fmt.Println(tm.Format("02/01/2006 15:04:05 PM"))
    
        //从字符串转为时间戳,第一个参数是格式,第二个是要转换的时间字符串
    
        tm2, _ := time.Parse("01/02/2006","02/08/2015")
    
        fmt.Println(tm2.Unix())
    
    }
    
    看了上面的代码,你可能会好奇,为什么格式字符串的时候,用的是2006-01-02这种格式。其实在Go  里,这些数字都是有特殊函义的,不是随便指定的数字,见下面列表:
    
    
    
    月份 1,01,Jan,January
    日  2,02,_2
    时  3,03,15,PM,pm,AM,am
    分  4,04
    秒  5,05
    年  06,2006
    周几 Mon,Monday
    时区时差表示 -07,-0700,Z0700,Z07:00,-07:00,MST
    时区字母缩写 MST
    
    
    package utils
    
    import (
        "time"
        "../commons"
        "strconv"
    )
    
    // 获取月初时间
    func GetEarlyMonthUnix() int64 {
        now := time.Now()
        tm := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
        return tm.Unix()
    }
    
    // 获取零时时间
    func GetZeroHourUnix() int64 {
        now := time.Now()
        tm := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
        return tm.Unix()
    }
    
    // 获取当前小时时间
    func GetNowHourUnix() int64 {
        now := time.Now()
        tm := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, now.Location())
        return tm.Unix()
    }
    
    // 获取当前时间
    func GetNowUnix() int64 {
        return time.Now().Unix()
    }
    
    // 获取年初时间
    func GetEarlyYearUnix() int64 {
        now := time.Now()
        tm := time.Date(now.Year(), 1, 1, 0, 0, 0, 0, now.Location())
        return tm.Unix()
    }
    
    func GetUnixToFormatString(timestamp int64, f string) string {
        tm := time.Unix(timestamp, 0)
        return tm.Format(f)
    }
    
    func GetUnixToString(timestamp int64) string {
        return GetUnixToFormatString(timestamp, "2006-01-02 00:00:00")
    }
    
    func GetUnixToHourString(timestamp int64) string {
        return GetUnixToFormatString(timestamp, "15:04")
    }
    
    func GetUnixToMonth(timestamp int64) string {
        tm := time.Unix(timestamp, 0)
        return commons.MonthMap[tm.Month().String()]
    }
    
    func GetUnixToDay(timestamp int64) int {
        tm := time.Unix(timestamp, 0)
        return tm.Day()
    }
    
    func GetUnixToDayTime(timestamp int64) string {
        month := GetUnixToMonth(timestamp)
        day := GetUnixToDay(timestamp)
        d := month + "." + strconv.Itoa(day)
        return d
    }
    
    func GetUnixToOldTime(i int) int64 {
        currentMonth := commons.MonthIntMap[time.Now().Month().String()]
    
        oldMonth := currentMonth - i
        if oldMonth < 0 {
            t := time.Date(time.Now().Year()-1, time.Now().Month()+1, 1, 0, 0, 0, 0, time.Local)
            return t.Unix()
        } else if oldMonth == 0 {
            t := time.Date(time.Now().Year(), 1, 1, 0, 0, 0, 0, time.Local)
            return t.Unix()
        }else if oldMonth > 0 && i >0{
            t := time.Date(time.Now().Year(), time.Now().Month()-1, 1, 0, 0, 0, 0, time.Local)
            return t.Unix()
        }else {
            t := time.Date(time.Now().Year(),time.Now().Month(),1,0,0,0,0,time.Local)
            return t.Unix()
        }
        return 0
    }
    
    func GetUnixToOldYearTime(i int) int64 {
        currentYear := time.Now().Year()
        oldMonth := currentYear - i
    
        t := time.Date(oldMonth, 1, 1, 0, 0, 0, 0, time.Local)
        return t.Unix()
    }
    //往前多少天时间戳
    func GetUnixToOldTimeDay(i int) int64 {
        day := time.Now().Day()
        oldMonth := day - i
        t := time.Date(time.Now().Year(), time.Now().Month(), oldMonth, time.Now().Hour(), time.Now().Minute(), time.Now().Second(), time.Now().Nanosecond(), time.Local)
        return t.Unix()
    }
    

    相关文章

      网友评论

          本文标题:Golang时间戳

          本文链接:https://www.haomeiwen.com/subject/uzpvbxtx.html