gotime

package module
v0.0.0-...-f7ba27e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 19, 2019 License: Apache-2.0 Imports: 1 Imported by: 2

README

golang 时间函数,快捷获取

做项目离不开跟时间打交道,如果有快捷的小函数使用,何乐而不为呢

安装

go get -u github.com/yezihack/gotime

使用方法,默认中国时间

t := gotime.New()
//获取当前时间
tm := t.Now()

# 时间区
* 默认中国东八区

# 使用方法
```golang
//获取当前时间
fmt.Println(gotime.Now())
//print 2018-12-12 15:16:19

//获取当前零晨时间
fmt.Println(gotime.NowStart())
//print 2018-12-12 00:00:00

//获取一个最后一刻
fmt.Println(gotime.NowEnd())
//print 2018-12-12 23:59:59

//获取当前时间戳
fmt.Println(gotime.NowUnix())
//1544599059

自定义时区

loc, err := time.LoadLocation("America/Cordoba")
if err != nil {
    panic(err)
}
g := GoTime{
    Location:loc,
}
fmt.Println(g.Now())
fmt.Println(g.GetRFC3339())

万能的format转换格式函数

//自定义格式的万能转换时间函数
tm := gotime.Format(gotime.NowTime(), "YYYY-MM-DD HH:mm:ss")
fmt.Println(tm)

//时间
tm1 := gotime.Format(gotime.NowTime(), "YYYY-MM-DD")
fmt.Println(tm1)

tm2 := gotime.Format(gotime.NowTime(), "HH:mm:ss")
fmt.Println(tm2)

tm3 := gotime.Format(gotime.NowTime(), "YYYY/MM/DD HH:mm:ss")
fmt.Println(tm3)

查询列表

函数名称 说明 实例
Now 获取当前时间 2018-12-12 15:22:22
NowUnix 获取当前时间戳 2018-12-12 15:22:22
NowTime 获取当前时间 time格式 2018-12-12 15:21:53.956340732 +0800 Asia/Shanghai
GetYmd 获取当前年月日 2018-12-12
GetHms 获取当前分时秒 15:22:22
NowStart 获取今天零晨时间 2018-12-12 00:00:00
NowEnd 获取今天最后一秒 2018-12-12 23:59:59
Before 时间相减,返回字符串 2018-12-12 15:22:22
Next 时间相加,返回字符串 2018-12-12 15:22:22
RfcToUnix RFC格式转时间戳 1544599696
GetRFC3339 获取RFC格式 2018-12-12T15:32:25+08:00
ToRFC3339 转换成RFC格式 2018-12-12T15:32:25+08:00
RFC3339To RFC格式转成正常格式 2018-12-12 15:22:22
GetFormat 获取格式化数据
Format 转换成任意格式

Documentation

Index

Constants

View Source
const (
	RFC3339 = "2006-01-02T15:04:05+08:00"
	TT      = "2006-01-02 15:04:05"
	YMD     = "2006-01-02"
	HMS     = "15:04:05"
)

常量

Variables

View Source
var BeijingLocation = time.FixedZone("Asia/Shanghai", 8*60*60)

北京东八时区

Functions

func Before

func Before(beforeSecond int64) string

当前时间 减去 多少秒

func End

func End(day int) string

获取当天的结束时间, eg: 2018-01-01 23:59:59

func Format

func Format(t time.Time, format string) string

Format time.Time struct to string MM - month - 01 M - month - 1, single bit DD - day - 02 D - day 2 YYYY - year - 2006 YY - year - 06 HH - 24 hours - 03 H - 24 hours - 3 hh - 12 hours - 03 h - 12 hours - 3 mm - minute - 04 m - minute - 4 ss - second - 05 s - second = 5

func GetFormat

func GetFormat(format string) string

func GetRFC3339

func GetRFC3339() string

获取RFC3339格式

func Hms

func Hms() string

获取时分秒

func Next

func Next(beforeSecond int64) string

当前时间 加上 多少秒

func Now

func Now() string

获取当前时间 年-月-日 时:分:秒

func NowTime

func NowTime() time.Time

获取当前时间Time

func NowUnix

func NowUnix() int64

获取当前时间戳

func ParseEnd

func ParseEnd(s, fromFormat string) (result string, err error)

将格式化为一天最后一刻

func ParseStart

func ParseStart(s, fromFormat string) (result string, err error)

将格式化为一天的零点

func RFC3339To

func RFC3339To(s string) string

将RFC3339格式转成正常格式

func RfcToUnix

func RfcToUnix(s string) int64

2006-01-02T15:04:05Z07:00 转 时间戳

func Start

func Start(day int) string

获取当天的开始时间, eg: 2018-01-01 00:00:00

func ToRFC3339

func ToRFC3339(s string) string

转换成RFC3339格式

func ToUnix

func ToUnix(s string) int64

2006-01-02 15:04:05 转 时间戳

func Ymd

func Ymd() string

获取年月日

Types

type GoTime

type GoTime struct {
	Location *time.Location
}

func NewBeijing

func NewBeijing() *GoTime

func (*GoTime) Before

func (g *GoTime) Before(beforeSecond int64) string

当前时间 减去 多少秒

func (*GoTime) End

func (g *GoTime) End(day int) string

获取当天的结束时间, eg: 2018-01-01 23:59:59

func (*GoTime) Format

func (g *GoTime) Format(t time.Time, format string) string

Format time.Time struct to string MM - month - 01 M - month - 1, single bit DD - day - 02 D - day 2 YYYY - year - 2006 YY - year - 06 HH - 24 hours - 03 H - 24 hours - 3 hh - 12 hours - 03 h - 12 hours - 3 mm - minute - 04 m - minute - 4 ss - second - 05 s - second = 5

func (*GoTime) GetFormat

func (g *GoTime) GetFormat(format string) string

获取格式化的数据

func (*GoTime) GetRFC3339

func (g *GoTime) GetRFC3339() string

获取RFC3339格式

func (*GoTime) Hms

func (g *GoTime) Hms() string

获取时分秒

func (*GoTime) Next

func (g *GoTime) Next(beforeSecond int64) string

当前时间 加上 多少秒

func (*GoTime) Now

func (g *GoTime) Now() string

获取当前时间 年-月-日 时:分:秒

func (*GoTime) NowTime

func (g *GoTime) NowTime() time.Time

获取当前时间Time

func (*GoTime) NowUnix

func (g *GoTime) NowUnix() int64

获取当前时间戳

func (*GoTime) ParseEnd

func (g *GoTime) ParseEnd(s, fromFormat string) (result string, err error)

格式化为一天的最后一刻

func (*GoTime) ParseStart

func (g *GoTime) ParseStart(s, fromFormat string) (result string, err error)

将格式化为一天的零点

func (*GoTime) RFC3339To

func (g *GoTime) RFC3339To(s string) string

将RFC3339格式转成正常格式

func (*GoTime) RfcToUnix

func (g *GoTime) RfcToUnix(s string) int64

2006-01-02T15:04:05Z07:00 转 时间戳

func (*GoTime) Start

func (g *GoTime) Start(day int) string

获取当天的开始时间, eg: 2018-01-01 00:00:00

func (*GoTime) ToRFC3339

func (g *GoTime) ToRFC3339(s string) string

转换成RFC3339格式

func (*GoTime) ToUnix

func (g *GoTime) ToUnix(s string) int64

2006-01-02 15:04:05 转 时间戳

func (*GoTime) Ymd

func (g *GoTime) Ymd() string

获取年月日

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL