Golang date to string, modeled after the date format structure in Java

Source: Internet
Author: User

 1, date format collection, date-to-string method

Package Utilimport ("Strings" "Time")//date format: Mimic struct in Java type Datestyle stringconst (mm_dd = "Mm-dd "Yyyymm =" yyyymm "yyyy_mm =" yyyy-mm "yyyy_mm_dd =" y                    Yyy-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 = "yy Yy/mm/dd HH:mm:ss.                   SSS "MM_DD_CN =" mm month DD Day "YYYY_MM_CN =" YYYY mm month "Yyyy_mm_dd_cn = "YYYY mm month dd Day" MM_DD_HH_MM_CN = "mm DD Day hh:mm" MM_DD_HH_MM_SS_CN = "mm month DD Day HH:MM:SS" yyyy_mm_                           DD_HH_MM_CN = "YYYY mm month DD Day hh:mm" YYYY_MM_DD_HH_MM_SS_CN = "YYYY year mm month DD Day HH:MM:SS" hh_mm = "hh:mm" HH_MM_SS = "HH:mm:ss" Hh_mm_ss_ms = "HH:mm:ss. SSS ")//date to string func formatdate (date time. Time, Datestyle Datestyle) string {layout: = string (datestyle) layout = strings. Replace (Layout, "yyyy", "2006", 1) layout = strings. Replace (Layout, "yy", ",", 1) layout = strings. Replace (Layout, "MM", "strings", 1) layout =. Replace (Layout, "DD", "strings", 1) layout =. Replace (Layout, ' HH ', ' 1 ', ' layout = Strings '). Replace (Layout, "MM", "04", 1) layout = strings. Replace (Layout, "SS", "on", 1) layout = strings. Replace (Layout, "SSS", "$",-1) return date. Format (Layout)}

2. Test code

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")}

Execution Result:

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 reads ∙1 likes
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.