• 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
  • 相关阅读:
    boost.property_tree的高级用法(你们没见过的操作)
    MFC- OnIdle空闲处理
    华为代码质量军规 (1) 数组访问,必须进行越界保护
    WinSocket 编程
    【C/C++】链表的理解与使用
    单链表
    C++ lambda表达式 (二)
    C++ lambda表达式 (一)
    C++11 volatile 类型
    关于结构体内存对齐方式的总结(#pragma pack()和alignas())
  • 原文地址:https://www.cnblogs.com/angelyan/p/11936463.html
Copyright © 2020-2023  润新知