12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- package util
- import "time"
- const (
- // TimeFmtLong yyyy-MM-dd hh:mm:ss
- TimeFmtLong = `2006-01-02 15:04:05`
- // TimeFmtNumeric yyyyMMddhhmmss
- TimeFmtNumeric = `20060102150405`
- // DateFmtLong yyyy-MM-dd
- DateFmtLong = `2006-01-02`
- // DateFmtNumeric yyyyMMdd
- DateFmtNumeric = `20060102`
- )
- // IsTime 是否时间格式字符串
- func IsTime(s string) bool {
- if _, err := time.Parse(TimeFmtLong, s); err != nil {
- return false
- }
- return true
- }
- // IsDate 是否为有效日期
- func IsDate(s string) bool {
- if _, e := time.Parse(`2006-01-02`, s); e != nil {
- return false
- }
- return true
- }
- // StrToTime 字符串转时间
- func StrToTime(s string) (t time.Time, err error) {
- t, err = time.Parse(TimeFmtLong, s)
- return
- }
- // StrFmtTime 时间转字符串
- func StrFmtTime(s, fmt string) (t time.Time, err error) {
- t, err = time.Parse(fmt, s)
- return
- }
- // TimeToStr 时间转字符串
- func TimeToStr(t time.Time) string {
- return t.Format(TimeFmtLong)
- }
- // TimeFmtStr 时间转字符串
- func TimeFmtStr(t time.Time, fmt string) string {
- return t.Format(fmt)
- }
- // StrToDate 字符串转日期
- func StrToDate(s string) (t time.Time, err error) {
- t, err = time.Parse(DateFmtLong, s)
- return
- }
- // IsWeekEnd 日期是否周末
- func IsWeekEnd(d time.Weekday) bool {
- day := int(d)
- if day == 6 || day == 0 {
- return true
- }
- return false
- }
|