golang 操作日期_使用Godate在Golang中轻松进行日期操作

golang 操作日期

by Kofo Okesola

由Kofo Okesola

使用Godate在Golang中轻松进行日期操作 (Easy date manipulation in Golang with Godate)

I 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)

The package is mostly a GoDate struct with its available helper methods, which acts as a wrapper to a Time struct. It also includes some functions for initializing, e.g. Now Tomorrow.

该软件包主要是带有可用帮助方法的GoDate结构,可作为Time结构的包装器。 它还包括一些初始化功能,例如Now Tomorrow

用法 (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结构

Note the difference in Timezone, which is why I recommend creating a GoDate struct with a time.Location object passed.

请注意时区的差异,这就是为什么我建议创建一个带有time.Location对象的GoDate结构的time.Location

Once you have a struct you can easily chain methods on the struct to achieve your result like so:

一旦有了结构,就可以轻松地在结构上链接方法以实现如下结果:

可用方法 (Available Methods)

比较 (Compare)

The available compare methods are IsBefore , IsBefore and IsWeekend . The method names explain what they do:

可用的比较方法是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:

最重要的差异方法将在下面突出显示。 尽管其中包括更多的方法,这些方法也用于这些方法的逻辑中:

The Difference methods that take another goDate as a parameter calculates the difference as methodOwner — parameter . A negative difference means the parameter occurs after the methodOwner.

以另一个goDate作为参数的Difference方法将Difference计算为methodOwner — parameter 。 负差表示参数出现在methodOwner

字符串格式 (String Formatting)

These are the current available String formatting methods. You can also format(you might want to read that if you’re new to dates in golang) this your way by calling the Format() method

这些是当前可用的String格式化方法。 您还可以格式 (你可能需要阅读,如果你是新来的golang日期)通过调用这个用自己的方式Format()方法

帮手 (Helper)

Some of the extra helper methods and their outputs are listed below:

下面列出了一些额外的辅助方法及其输出:

Note the EndOfWeek and StartOfWeek methods use time.Sunday as the default start of the week. This behavior can be changed for the current godate struct by calling now.SetFirstDay(time.Monday) .

请注意, EndOfWeekStartOfWeek方法使用time.Sunday作为默认的一周开始时间。 可以通过调用now.SetFirstDay(time.Monday)来为当前的Godate结构更改此行为。

结论 (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(和星星:)

翻译自: https://www.freecodecamp.org/news/easy-date-manipulation-in-golang-with-godate-485eef7254a0/

golang 操作日期