1、日期格式集合、日期转字符串方法
package util
import (
"strings"
"time"
)
//日期格式:模仿java中的结构体
type DateStyle string
const (
MM_DD = "MM-dd"
YYYYMM = "yyyyMM"
YYYY_MM = "yyyy-MM"
YYYY_MM_DD = "yyyy-MM-dd"
YYYYMMDD = "yyyyMMdd"
YYYYMMDDHHMMSS = "yyyyMMddHHmmss"
YYYYMMDDHHMM = "yyyyMMddHHmm"
YYYYMMDDHH = "yyyyMMddHH"
YYMMDDHHMM = "yyMMddHHmm"
MM_DD_HH_MM = "MM-dd HH:mm"
MM_DD_HH_MM_SS = "MM-dd HH:mm:ss"
YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm"
YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss"
YYYY_MM_DD_HH_MM_SS_SSS = "yyyy-MM-dd HH:mm:ss.SSS"
MM_DD_EN = "MM/dd"
YYYY_MM_EN = "yyyy/MM"
YYYY_MM_DD_EN = "yyyy/MM/dd"
MM_DD_HH_MM_EN = "MM/dd HH:mm"
MM_DD_HH_MM_SS_EN = "MM/dd HH:mm:ss"
YYYY_MM_DD_HH_MM_EN = "yyyy/MM/dd HH:mm"
YYYY_MM_DD_HH_MM_SS_EN = "yyyy/MM/dd HH:mm:ss"
YYYY_MM_DD_HH_MM_SS_SSS_EN = "yyyy/MM/dd HH:mm:ss.SSS"
MM_DD_CN = "MM月dd日"
YYYY_MM_CN = "yyyy年MM月"
YYYY_MM_DD_CN = "yyyy年MM月dd日"
MM_DD_HH_MM_CN = "MM月dd日 HH:mm"
MM_DD_HH_MM_SS_CN = "MM月dd日 HH:mm:ss"
YYYY_MM_DD_HH_MM_CN = "yyyy年MM月dd日 HH:mm"
YYYY_MM_DD_HH_MM_SS_CN = "yyyy年MM月dd日 HH:mm:ss"
HH_MM = "HH:mm"
HH_MM_SS = "HH:mm:ss"
HH_MM_SS_MS = "HH:mm:ss.SSS"
)
//日期转字符串
func FormatDate(date time.Time, dateStyle DateStyle) string {
layout := string(dateStyle)
layout = strings.Replace(layout, "yyyy", "2006", 1)
layout = strings.Replace(layout, "yy", "06", 1)
layout = strings.Replace(layout, "MM", "01", 1)
layout = strings.Replace(layout, "dd", "02", 1)
layout = strings.Replace(layout, "HH", "15", 1)
layout = strings.Replace(layout, "mm", "04", 1)
layout = strings.Replace(layout, "ss", "05", 1)
layout = strings.Replace(layout, "SSS", "000", -1)
return date.Format(layout)
}
2、测试代码
package main
import (
"fmt"
"time"
"util"
)
func main() {
fmt.Println(util.FormatDate(time.Now(), util.HH_MM))
fmt.Println(util.FormatDate(time.Now(), util.HH_MM_SS))
fmt.Println(util.FormatDate(time.Now(), util.HH_MM_SS_MS))
fmt.Println(util.FormatDate(time.Now(), util.MM_DD))
fmt.Println(util.FormatDate(time.Now(), util.MM_DD_CN))
fmt.Println(util.FormatDate(time.Now(), util.MM_DD_EN))
fmt.Println(util.FormatDate(time.Now(), util.MM_DD_HH_MM))
fmt.Println(util.FormatDate(time.Now(), util.MM_DD_HH_MM_CN))
fmt.Println(util.FormatDate(time.Now(), util.MM_DD_HH_MM_EN))
fmt.Println(util.FormatDate(time.Now(), util.MM_DD_HH_MM_SS))
fmt.Println(util.FormatDate(time.Now(), util.MM_DD_HH_MM_SS_CN))
fmt.Println(util.FormatDate(time.Now(), util.MM_DD_HH_MM_SS_EN))
fmt.Println(util.FormatDate(time.Now(), util.YYMMDDHHMM))
fmt.Println(util.FormatDate(time.Now(), util.YYYYMM))
fmt.Println(util.FormatDate(time.Now(), util.YYYYMMDD))
fmt.Println(util.FormatDate(time.Now(), util.YYYYMMDDHH))
fmt.Println(util.FormatDate(time.Now(), util.YYYYMMDDHHMM))
fmt.Println(util.FormatDate(time.Now(), util.YYYYMMDDHHMMSS))
fmt.Println(util.FormatDate(time.Now(), util.YYYY_MM))
fmt.Println(util.FormatDate(time.Now(), util.YYYY_MM_CN))
fmt.Println(util.FormatDate(time.Now(), util.YYYY_MM_DD))
fmt.Println(util.FormatDate(time.Now(), util.YYYY_MM_DD_CN))
fmt.Println(util.FormatDate(time.Now(), util.YYYY_MM_DD_EN))
fmt.Println(util.FormatDate(time.Now(), util.YYYY_MM_DD_HH_MM))
fmt.Println(util.FormatDate(time.Now(), util.YYYY_MM_DD_HH_MM_CN))
fmt.Println(util.FormatDate(time.Now(), util.YYYY_MM_DD_HH_MM_EN))
fmt.Println(util.FormatDate(time.Now(), util.YYYY_MM_DD_HH_MM_SS))
fmt.Println(util.FormatDate(time.Now(), util.YYYY_MM_DD_HH_MM_SS_CN))
fmt.Println(util.FormatDate(time.Now(), util.YYYY_MM_DD_HH_MM_SS_EN))
fmt.Println(util.FormatDate(time.Now(), util.YYYY_MM_DD_HH_MM_SS_SSS))
fmt.Println(util.FormatDate(time.Now(), util.YYYY_MM_DD_HH_MM_SS_SSS_EN))
fmt.Println(util.FormatDate(time.Now(), util.YYYY_MM_EN))
fmt.Println(util.FormatDate(time.Now(), "yyyy/MM/dd HH:mm:ss.SSSSSSSSS"))
fmt.Println(util.FormatDate(time.Now(), "2006 01 02 15:04:05.00000000"))
}
执行结果:
15:30
15:30:57
15:30:57.577
06-06
06月06日
06/06
06-06 15:30
06月06日 15:30
06/06 15:30
06-06 15:30:57
06月06日 15:30:57
06/06 15:30:57
1806061530
201806
20180606
2018060615
201806061530
20180606153057
2018-06
2018年06月
2018-06-06
2018年06月06日
2018/06/06
2018-06-06 15:30
2018年06月06日 15:30
2018/06/06 15:30
2018-06-06 15:30:57
2018年06月06日 15:30:57
2018/06/06 15:30:57
2018-06-06 15:30:57.577
2018/06/06 15:30:57.577
2018/06
2018/06/06 15:30:57.577065200
2018 06 06 15:30:57.57706520
有疑问加站长微信联系(非本文作者)