• go指定分隔符格式化时间


    一、代码

    package main
    
    import (
        "fmt"
        "strings"
        "strconv"
        "time"
    )
    
    // StrToIntMonth 字符串月份转整数月份
    func StrToIntMonth(month string) int  {
        var data = map[string]int{
            "January"   : 0,
            "February"  : 1,
            "March"     : 2,
            "April"     : 3,
            "May"       : 4,
            "June"      : 5,
            "July"      : 6,
            "August"    : 7,
            "September" : 8,
            "October"   : 9,
            "November"  : 10,
            "December"  : 11,
        };
        return data[month];
    }
    
    // GetTodayYMD 得到以sep为分隔符的年、月、日字符串(今天)
    func GetTodayYMD(sep string) string {
        now     := time.Now()
        year    := now.Year()
        month   := StrToIntMonth(now.Month().String())
        date    := now.Day()
    
        var monthStr string
        var dateStr string
        if month < 9 {
            monthStr = "0" + strconv.Itoa(month + 1)
        } else {
            monthStr = strconv.Itoa(month + 1)
        }
    
        if date < 10 {
            dateStr = "0" + strconv.Itoa(date)
        } else {
            dateStr = strconv.Itoa(date)
        }
        return strconv.Itoa(year) + sep + monthStr + sep + dateStr
    }
    
    // GetTodayYM 得到以sep为分隔符的年、月字符串(今天所属于的月份)
    func GetTodayYM(sep string) string {
        now     := time.Now()
        year    := now.Year()
        month   := StrToIntMonth(now.Month().String())
    
        var monthStr string
        if month < 9 {
            monthStr = "0" + strconv.Itoa(month + 1)
        } else {
            monthStr = strconv.Itoa(month + 1)
        }
        return strconv.Itoa(year) + sep + monthStr
    }
    
    // GetYesterdayYMD 得到以sep为分隔符的年、月、日字符串(昨天)
    func GetYesterdayYMD(sep string) string {
        now           := time.Now()
        today         := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
        todaySec      := today.Unix() //秒
        yesterdaySec  := todaySec - 24 * 60 * 60; //秒
        yesterdayTime := time.Unix(yesterdaySec, 0)
        yesterdayYMD  := yesterdayTime.Format("2006-01-02")
        return strings.Replace(yesterdayYMD, "-", sep, -1)
    }
    
    // GetTomorrowYMD 得到以sep为分隔符的年、月、日字符串(明天)
    func GetTomorrowYMD(sep string) string {
        now           := time.Now()
        today         := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
        todaySec      := today.Unix() //秒
        tomorrowSec   := todaySec + 24 * 60 * 60; //秒
        tomorrowTime  := time.Unix(tomorrowSec, 0)
        tomorrowYMD   := tomorrowTime.Format("2006-01-02")
        return strings.Replace(tomorrowYMD, "-", sep, -1)
    }
    
    func main() {
        //当天
        res0:=GetTodayYMD("*")
        fmt.Println(res0)
        //本月
        res1:=GetTodayYM("-")
        fmt.Println(res1)
        //昨天
        res2:=GetYesterdayYMD(".")
        fmt.Println(res2)
        //明天
        res3:=GetTomorrowYMD(" ")
        fmt.Println(res3)
    }

    二、结果

    2019*11*26
    2019-11
    2019.11.25
    2019 11 27
  • 相关阅读:
    Window7幻灯片字体显示混乱,难道真的是病毒么
    COCOS2DX 3.0 优化提升渲染速度 Auto-batching
    iOS 打印出视图中全部的子视图的名称
    【linux】学习2
    【编程之美】2.16 求数组的最大递增子序列
    【linux】学习1
    【编程之美】2.15 子数组之和的最大值(二维)
    【编程之美】2.14 求数组的子数组之和的最大值
    【QT】视频播放
    【编程之美】3.5 最短摘要的生成
  • 原文地址:https://www.cnblogs.com/angelyan/p/11936463.html
Copyright © 2020-2023  润新知