1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- 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.ParseInLocation(TimeFmtLong, s, time.Local)
- t, err = time.Parse(TimeFmtLong, s)
- return
- }
- // StrFmtTime 时间转字符串
- func StrFmtTime(s, fmt string) (t time.Time, err error) {
- //t, err = time.ParseInLocation(fmt, s, time.Local)
- 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.ParseInLocation(DateFmtLong, s, time.Local)
- 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
- }
|