You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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