1、日期格式集合、日期轉字串方法
package utilimport ("strings""time")//日期格式:模仿java中的結構體type DateStyle stringconst (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 mainimport ( "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:3015:30:5715:30:57.57706-0606月06日06/0606-06 15:3006月06日 15:3006/06 15:3006-06 15:30:5706月06日 15:30:5706/06 15:30:571806061530201806201806062018060615201806061530201806061530572018-062018年06月2018-06-062018年06月06日2018/06/062018-06-06 15:302018年06月06日 15:302018/06/06 15:302018-06-06 15:30:572018年06月06日 15:30:572018/06/06 15:30:572018-06-06 15:30:57.5772018/06/06 15:30:57.5772018/062018/06/06 15:30:57.5770652002018 06 06 15:30:57.57706520
275 次點擊 ∙ 1 贊