内容简介:golang 时间格式化要指定默认的格式,如果不指定默认的格式就会出现错误,如果一个未知的时间格式需要做格式化,是比较棘手的。下面是我在项目写的一个时间格式化函数,简单的支持几种格式测试
golang 时间格式化要指定默认的格式,如果不指定默认的格式就会出现错误,如果一个未知的时间格式需要做格式化,是比较棘手的。
下面是我在项目写的一个时间格式化函数,简单的支持几种格式
package util import ( "strings" "time" ) func TimeParseLocal(in string) (out time.Time, err error) { birStrLen := strings.Count(in, "") - 1 switch birStrLen { case 10: //2006-01-02 return TimeParse10(in) case 19: //2006-01-02 15:04:05 return TimeParse19(in) default: } return } // yyyy-mm-dd | mm-dd-yyyy 格式 func TimeParse10(in string) (out time.Time, err error) { chars := []rune(in) fc := "" for k, char := range chars { if char == '-' || char == '/' || char == ':' || char == '.' { fc = string(char) } switch { case (k == 0) && ((char <= ('Z') && char >= ('A')) || (char <= ('z') && char >= ('a'))): // 这个是 Jun-MM-YYYY 格式 case (k == 2) && (char == '-' || char == '/' || char == ':' || char == '.'): return TimeParseMMDDYYYY(in, string(char)) case (k == 4) && (char == '-' || char == '/' || char == ':' || char == '.'): return TimeParseYYYYMMDD(in, string(char)) } } return TimeParseYYYYMMDD(in, fc) } // yyyy-mm-dd hh:mm:ss | mm-dd-yyyy hh:mm:ss 格式 func TimeParse19(in string) (out time.Time, err error) { chars := []rune(in) fc := "" for k, char := range chars { if char == '-' || char == '/' || char == ':' || char == '.' { fc = string(char) } switch { case (k == 0) && ((char <= ('Z') && char >= ('A')) || (char <= ('z') && char >= ('a'))): // 这个是 Jun-MM-YYYY 格式 case (k == 2) && (char == '-' || char == '/' || char == ':' || char == '.'): return TimeParseMMDDYYYYHHMMSS(in, string(char)) case (k == 4) && (char == '-' || char == '/' || char == ':' || char == '.'): return TimeParseYYYYMMDDHHMMSS(in, string(char)) } } return TimeParseYYYYMMDDHHMMSS(in, fc) } func TimeParseYYYYMMDD(in string, sub string) (out time.Time, err error) { layout := "2006" + sub + "01" + sub + "02" out, err = time.ParseInLocation(layout, in, time.Local) if err != nil { return } return } func TimeParseMMDDYYYY(in string, sub string) (out time.Time, err error) { layout := "01" + sub + "02" + sub + "2006" out, err = time.ParseInLocation(layout, in, time.Local) if err != nil { return } return } func TimeParseYYYYMMDDHHMMSS(in string, sub string) (out time.Time, err error) { layout := "2006" + sub + "01" + sub + "02 15:04:05" out, err = time.ParseInLocation(layout, in, time.Local) if err != nil { return } return } func TimeParseMMDDYYYYHHMMSS(in string, sub string) (out time.Time, err error) { layout := "01" + sub + "02" + sub + "2006 15:04:05" out, err = time.ParseInLocation(layout, in, time.Local) if err != nil { return } return }
测试
package util import ( "testing" ) func TestTimeParseLocal(t *testing.T) { t.Log(TimeParseLocal("2019/03/26")) t.Log(TimeParseLocal("03/26/2019")) t.Log(TimeParseLocal("2019/03/26 17:30:03")) t.Log(TimeParseLocal("03/26/2019 17:30:03")) }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Principles of Object-Oriented JavaScript
Nicholas C. Zakas / No Starch Press / 2014-2 / USD 24.95
If you've used a more traditional object-oriented language, such as C++ or Java, JavaScript probably doesn't seem object-oriented at all. It has no concept of classes, and you don't even need to defin......一起来看看 《Principles of Object-Oriented JavaScript》 这本书的介绍吧!