• golang日期转字符串,仿照C#中的日期格式结构体


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

      

  • 相关阅读:
    codevs 1199 开车旅行 2012年NOIP全国联赛提高组
    poj 3349 Snowflake Snow Snowflakes
    poj 3264 Balanced Lineup
    求二进制数中1的个数
    20个正则表达式,减少千行代码
    推流脚本
    Navicat 管理工具下载地址,破解版
    emoji表情处理研究
    计算机网络学习
    tcp/ip 学习-通过视频学习
  • 原文地址:https://www.cnblogs.com/dfsxh/p/10248941.html
Copyright © 2020-2023  润新知