package DateUtil import "time" /** 功能:获取当前的时间字符串 作者:黄海 时间:2020-01-20 */ func GetCurrentTimeStr() string { return time.Now().Format("20060102150405") } const TimeLayoutStr = "2006-01-02 15:04:05" //go中的时间格式化必须是这个时间 /** 功能:将一个字符串转为对应的时间 作者:黄海 时间:2020-05-29 */ func ConvertDateTime(ts string) time.Time { st, _ := time.Parse(TimeLayoutStr, ts) //string转time return st } func ConvertDate(ts string) time.Time { //如果是带时分秒的 if len(TimeLayoutStr) == len(ts) { return ConvertDateTime(ts) } else { // 不带时分秒的补全 return ConvertDateTime(ts + " 00:00:00") } } /** 功能:判断一个字符串是不是合法的日期格式 作者:黄海 时间:2020-08-18 */ func CheckDateStr(s string) bool { //这种情况下,time.Parse会转成时间2014-03-01 00:00:00,有一个办法是,转换后如果没有报错,你再Format, //跟原来的的对比一下,如果不同,那就可以是说是错误的。 t, err := time.Parse("2006-01-02 15:04:05", s) if err != nil { return false } if s == t.Format("2006-01-02") { return true } else { return false } }