github.com/keysonZZZ/kmg@v0.0.0-20151121023212-05317bfd7d39/kmgTime/Format.go (about) 1 package kmgTime 2 3 import "time" 4 5 const ( 6 FormatMysqlZero = "0000-00-00 00:00:00" 7 FormatMysql = "2006-01-02 15:04:05" 8 FormatMysqlUs = "2006-01-02 15:04:05.999999" 9 FormatFileName = "2006-01-02_15-04-05" //适合显示在文件上面的日期格式 @deprecated 10 FormatFileNameV2 = "2006-01-02-15-04-05" //版本2,更规整,方便使用正则取出 11 FormatDateMysql = "2006-01-02" 12 Iso3339Hour = "2006-01-02T15" 13 Iso3339Minute = "2006-01-02T15:04" 14 Iso3339Second = "2006-01-02T15:04:05" 15 AppleJsonFormat = "2006-01-02 15:04:05 Etc/MST" //仅解决GMT的这个特殊情况.其他不管,如果苹果返回的字符串换时区了就悲剧了 16 17 FormatMysqlMinute = "2006-01-02 15:04" 18 FormatMysqlMouthAndDay = "01-02" 19 FormatMysqlYearAndMoney = "2006-01" 20 ) 21 22 var ParseFormatGuessList = []string{ 23 FormatMysqlZero, 24 FormatMysql, 25 FormatDateMysql, 26 Iso3339Hour, 27 Iso3339Minute, 28 Iso3339Second, 29 time.RFC3339, 30 time.RFC3339Nano, 31 } 32 33 var MysqlStart = "0000-01-01 00:00:00" 34 var MysqlEnd = "9999-12-31 23:59:59" 35 36 //输出成mysql的格式,并且使用默认时区,并且在0值的时候输出空字符串 37 func DefaultFormat(t time.Time) string { 38 if t.IsZero() { 39 return "" 40 } 41 return t.In(DefaultTimeZone).Format(FormatMysql) 42 } 43 44 func MonthAndDayFormat(t time.Time) string { 45 if t.IsZero() { 46 return "" 47 } 48 return t.In(DefaultTimeZone).Format(FormatMysqlMouthAndDay) 49 } 50 51 func NowWithFileNameFormatV2() string { 52 return NowFromDefaultNower().Format(FormatFileNameV2) 53 }