go delimiter format specified time

First, the code

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 the]; 
}

 // GetTodayYMD get to sep as the delimiter year, month, day string (today)
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) 
    } the else { 
        dateStr = strconv.Itoa (DATE) 
    } 
    return strconv.Itoa (year) + sep sep + + + monthStr dateStr 
}

 // GetTodayYM sep obtained as a delimiter of, month string (today belongs month) 
FUNC GetTodayYM (sep string) {string 
    now: = Time.now () 
    year: = now.Year () 
    month the: = StrToIntMonth (. now.Month () string ()) 

    var String monthStr 
    IF month The <. 9 { 
        monthStr = " 0" + Strconv.Itoa (month The +. 1 ) 
    } the else  {
        monthStr = strconv.Itoa (month The +. 1 ) 
    } 
    return strconv.Itoa (year) + sep + monthStr 
}

 // GetYesterdayYMD sep obtained as a delimiter of the year, month, day string (yesterday) 
FUNC GetYesterdayYMD (On Sep string) string { 
    now: = Time.now () 
    Today: = time.Date (now.Year (), now.Month (), now.Day (), 0, 0, 0, 0, Time.local) 
    todaySec: = today.Unix () // sec 
    yesterdaySec: = todaySec - 24 * 60 * 60; // second 
    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)
    // Tomorrow 
    RES3: =GetTomorrowYMD("  " ) 
    fmt.Println (RES3) 
}

Second, the results

2019*11*26
2019-11
2019.11.25
2019 11 27

 

Guess you like

Origin www.cnblogs.com/angelyan/p/11936463.html