123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- package tyr
- import (
- "time"
- )
- // Time fmt
- const (
- TimeFmtLong = `2006-01-02 15:04:05` // yyyy-MM-dd hh:mm:ss
- TimeFmtNumeric = `20060102150405` // yyyyMMddhhmmss
- DateFmtLong = `2006-01-02` // yyyy-MM-dd
- DateFmtNumeric = `20060102` // yyyyMMdd
- )
- // 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(DateFmtLong, s); e != nil {
- return false
- }
- return true
- }
- // Str2Date 字符串转日期
- func Str2Date(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
- }
- // Str2Time 字符串转时间
- func Str2Time(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
- }
- // Time2Str 时间转字符串
- func Time2Str(t time.Time) string {
- return t.Format(TimeFmtLong)
- }
- // TimeFmtStr 时间转字符串
- func TimeFmtStr(t time.Time, fmt string) string {
- return t.Format(fmt)
- }
|