go Time操作笔记

package time_util

import (
	"fmt"
	"time"
)

//TimeFormat 时间格式
type TimeFormat int

const (
	timeTeplate1 TimeFormat = iota
	timeTeplate2
	timeTeplate3
	timeTeplate4
	timeTeplate5
)

func (t TimeFormat) String() string {
	switch t {
	case 0:
		return "2006-01-02 15:04:05" //yyyy-MM-dd HH:mm:ss
	case 1:
		return "2006/01/02 15:04:05" //yyyy/MM/dd HH:mm:ss
	case 2:
		return "2006-01-02" //yyyy-MM-dd
	case 3:
		return "2006/01/02" //yyyy/MM/dd
	case 4:
		return "15:04:05" //HH:mm:ss
	default:
		return "时间格式未定义"
	}
}

//Format 时间转字符串
func Format(times time.Time, f TimeFormat) string {
	return times.Format(f.String())
}

//UnixFormat 时间戳转字符串
func UnixFormat(sec int64, f TimeFormat) string {
	return time.Unix(sec, 0).Format(f.String())
}

//ParseInlocation 字符串转时间
func ParseInlocation(str string, f TimeFormat) (time.Time, error) {
	return time.ParseInLocation(f.String(), str, time.Local)
}

//Now 获取当前时间
func Now() time.Time {
	return time.Now()
}

//Unix 时间转成时间戳(秒)
func Unix(times time.Time) int64 {
	return times.Unix()
}

//UnixNano 时间转成时间戳(毫秒)
func UnixNano(times time.Time) int64 {
	return times.UnixNano() / 1e6
}

//UnixToTime 时间戳转化成时间
func UnixToTime(timestamp int64) time.Time {
	return time.Unix(timestamp, 0)
}

//StringToUnix 字符串转时间戳(秒)
func StringToUnix(str string, f TimeFormat) int64 {
	ts, _ := ParseInlocation(str, f)
	return Unix(ts)
}

//StringToUnixNano 字符串转时间戳(毫秒)
func StringToUnixNano(str string, f TimeFormat) int64 {
	ts, _ := ParseInlocation(str, f)
	return UnixNano(ts)
}

//Add 添加日期
func Add(ts time.Time, d time.Duration) time.Time {
	return ts.Add(d)
}

//AddDate 添加日期
func AddDate(ts time.Time, years, months, days int) time.Time {
	return ts.AddDate(years, months, days)
}

func main() {
	x := Now()
	fmt.Println(x)

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值