golang日期轉字串,仿照java中的日期格式結構體

來源:互聯網
上載者:User

 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 贊  
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.