123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- package myth
- 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
- }
|