zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    大战设计模式【5】—— 工厂方法模式
    通过spring抽象路由数据源+MyBatis拦截器实现数据库自动读写分离
    大战设计模式【4】—— 简单工厂模式
    大战设计模式【3】—— 装饰模式
    大战设计模式【2】—— 观察者模式
    大战设计模式【1】—— 策略模式
    回顾:maven配置和常用命令整理
    idea properties文件unicode码问题
    Nginx学习笔记
    tomcat添加context方式部署web应用
  • 原文地址:https://www.cnblogs.com/angelyan/p/11936463.html
Copyright © 2011-2022 走看看