golang 操作日期
by Kofo Okesola
由Kofo Okesola
使用Godate在Golang中轻松进行日期操作 Easy date manipulation in Golang with GodateI have always been and always will be a fan of Carbon and how easy it is to get date manipulation done so efficiently. Being a fan of Carbon and also being a fan of Golang I thought why not write a library called godate. It will do for golang what carbon does for Php, and in this article I will explain how to use it.
我一直并且永远都是Carbon的粉丝,如此高效地完成日期操作有多么容易。 作为Carbon的粉丝和Golang的粉丝,我想为什么不编写一个名为godate的库 。 它将对golang起作用,就像碳对Php起作用,在本文中,我将解释如何使用它。
包装明细 (Package Breakdown)
GoDateTimeNowTomorrow
GoDateTimeNowTomorrow
用法 (Usage)
安装 (Installation)
go get github.com/kofoworola/godate
It also supports go’s new module system. You can simply import it into your project and run. Go will attempt to install the latest version of the package, which is v1.2.0 as of the time of writing this.
它还支持go的新模块系统 。 您可以简单地将其导入您的项目并运行。 Go会尝试安装该软件包的最新版本,在撰写本文时为v1.2.0。
用法 (Usage)
Create a new GoDate struct with any of the methods currently available
使用当前可用的任何方法创建一个新的GoDate结构
time.Location
time.Locationtime.Location
Once you have a struct you can easily chain methods on the struct to achieve your result like so:
一旦有了结构,就可以轻松地在结构上链接方法以实现如下结果:
可用方法 (Available Methods)
比较 (Compare)
IsBeforeIsBeforeIsWeekend
IsBeforeIsBeforeIsWeekend
区别 (Difference)
The most important Difference methods are highlighted below. Although there are more methods included that are also used in the logic of these:
最重要的差异方法将在下面突出显示。 尽管其中包括更多的方法,这些方法也用于这些方法的逻辑中:
DifferencegoDatemethodOwner — parametermethodOwner
goDateDifferenceDifferencemethodOwner — parametermethodOwner
字符串格式 (String Formatting)
Format()
Format()
帮手 (Helper)
Some of the extra helper methods and their outputs are listed below:
下面列出了一些额外的辅助方法及其输出:
EndOfWeekStartOfWeektime.Sundaynow.SetFirstDay(time.Monday)
EndOfWeekStartOfWeektime.Sundaynow.SetFirstDay(time.Monday)
结论 (Conclusion)
The package is far from complete (and probably never will be). The aim is to provide a robust date handling API similar to and even better (someone’s ambitious here…) than Carbon. So you Go lovers out there like me should make it rain PRs on the repo (and stars :)
该软件包远未完成(可能永远不会完成)。 目的是提供一个健壮的日期处理API,与Carbon相似,甚至更好(有人在这里雄心勃勃…)。 所以像我这样的恋人应该在回购协议上放下PR(和星星:)
golang 操作日期