Go语言教程之边写边学:常用的软件库:时间工具包

now是一个非常简单的包,它为标准时间包提供了一个方便的包装器,并使其易于处理当前时间周围的各种日期和时间构造。

 

安装软件包:

go get -u github.com/jinzhu/now

 

示例代码:

package main

import (
	"fmt"
	"time"

	"github.com/jinzhu/now"
)

func main() {
	fmt.Println("BeginningOfMinute \t", now.BeginningOfMinute())
	fmt.Println("BeginningOfHour \t", now.BeginningOfHour())
	fmt.Println("BeginningOfDay  \t", now.BeginningOfDay())
	fmt.Println("BeginningOfWeek \t", now.BeginningOfWeek())
	fmt.Println("BeginningOfMonth \t", now.BeginningOfMonth())
	fmt.Println("BeginningOfQuarter \t", now.BeginningOfQuarter())
	fmt.Println("BeginningOfYear \t", now.BeginningOfYear())
	fmt.Println()

	fmt.Println("EndOfMinute \t", now.EndOfMinute())
	fmt.Println("EndOfHour \t", now.EndOfHour())
	fmt.Println("EndOfDay  \t", now.EndOfDay())
	fmt.Println("EndOfWeek \t", now.EndOfWeek())
	fmt.Println("EndOfMonth \t", now.EndOfMonth())
	fmt.Println("EndOfQuarter \t", now.EndOfQuarter())
	fmt.Println("EndOfYear \t", now.EndOfYear())
	fmt.Println()

	fmt.Println("Monday 	\t", now.Monday())
	fmt.Println("Sunday 	\t", now.Sunday())
	fmt.Println("EndOfSunday \t", now.EndOfSunday())
	fmt.Println()

	fmt.Println(now.Parse("2017"))
	fmt.Println(now.Parse("2017-12-12 12:20"))

	t := time.Date(2020, 07, 18, 17, 51, 49, 123456789, time.Now().Location())
	fmt.Println(now.With(t).EndOfMonth())
}

 

输出:

BeginningOfMinute        2020-07-19 16:55:00 +0530 IST
BeginningOfHour          2020-07-19 16:00:00 +0530 IST
BeginningOfDay           2020-07-19 00:00:00 +0530 IST
BeginningOfWeek          2020-07-19 00:00:00 +0530 IST
BeginningOfMonth         2020-07-01 00:00:00 +0530 IST
BeginningOfQuarter       2020-07-01 00:00:00 +0530 IST
BeginningOfYear          2020-01-01 00:00:00 +0530 IST

EndOfMinute      2020-07-19 16:55:59.999999999 +0530 IST
EndOfHour        2020-07-19 16:59:59.999999999 +0530 IST
EndOfDay         2020-07-19 23:59:59.999999999 +0530 IST
EndOfWeek        2020-07-25 23:59:59.999999999 +0530 IST
EndOfMonth       2020-07-31 23:59:59.999999999 +0530 IST
EndOfQuarter     2020-09-30 23:59:59.999999999 +0530 IST
EndOfYear        2020-12-31 23:59:59.999999999 +0530 IST

Monday           2020-07-13 00:00:00 +0530 IST
Sunday           2020-07-19 00:00:00 +0530 IST
EndOfSunday      2020-07-19 23:59:59.999999999 +0530 IST

2017-01-01 00:00:00 +0530 IST 
2017-12-12 12:20:00 +0530 IST 
2020-07-31 23:59:59.999999999 +0530 IST