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 操作日期

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

  1. sql server 中获取前一天日期_图解SQL面试题:如何比较日期数据?

    ​[题目] 下面是某公司每天的营业额,表名为"日销"."日期"这一列的数据类型是日期类型(date). 请找出所有比前一天(昨天)营业额更高的数据.(前一天的意 ...

  2. 电脑表格日期怎么修改原有日期_如何在Excel表中自动生成记录数据的日期和时间...

    几天前有人问我: 在Excel表格中输入信息时,如何在日期单元格中自动生成日期和时间? 输入表格时,我们经常需要输入时间,而不是填写存储时间,为了减少输入工作量,您可以将日期设置为自动生成. 例如,当 ...

  3. mysql不可对视图的操作_在数据表视图中不能进行的操作是什么

    在数据表视图中不能进行的操作是什么? 在数据表视图中不能进行的操作是修改字段的类型. 在"数据表"视图中可以进行字段的编辑.添加.删除,记录的删除和数据查找等操作,但使用" ...

  4. 华为怎么分屏操作技巧_第五人格昆虫学家虫群怎么用 昆虫学家操作技巧介绍...

    第五人格中昆虫学家怎么在游戏中操作?知道昆虫学家的主要作用后下一步就是要掌握在游戏中的操作技巧,那么要怎么操作才能更好的帮助同伴赢得比赛呢?下面小编就整理了关于第五人格中昆虫学家的操作技巧,一起来看一 ...

  5. JVM学习笔记之-StringTable String的基本特性,内存分配,基本操作,拼接操作,intern()的使用,垃圾回收 ,G1中的String去重操作

    String的基本特性 string:字符串,使用一对""引起来表示. String s1 = ""; //字面量的定义方式 String s2 = new S ...

  6. golang mysql封装_自己封装的golang 操作数据库方法

    这个是我用Go写的第一个东东,可能还存在些BUG没有测试到.这里主要是提供一个参考.各位可以改写成自己的风格. 在命令行下输入如下两条命令,进行安装 go get github.com/male110 ...

  7. golang 切片 接口_一日看尽golang高级语法之slice

    golang系列的文章包含多篇文章,总篇如下,其中包含各篇文章的指引 明月映江雪:golang系列--个人学习笔记总篇​zhuanlan.zhihu.com 由于最近事情比较多,拖到现在才更新,另一方 ...

  8. wps斜杠日期格式_在WPS表格中轻松统一日期格式

    平常办公中经常需要收集汇总下级各部门上报的数据表格.由于各部门输入人员并没有按统一标准格式输入日期,汇总数据之后往往会发现汇总数据中的日期有很多种格式.除了2009-2-3.1978年5月6日等标准格 ...

  9. mysql 操作审计_利用mysql的audit审计功能记录用户操作信息

    mysql数据库中我们如果想记录用户的操作信息,可以通过audit审计功能来来实现.该功能是被自动触发的,在文件plugin_audit.h中可以看到比较详细的定义.在audit插件中,可控制的变量包 ...

最新文章

  1. nokia 上的好玩应用(转载)
  2. MySql数据备份与恢复
  3. 读取当前linux进程内存_(笔记)Linux上的内存分配
  4. 高效管理论坛广告贴的小窍门
  5. GD2拖动验证码Thinkphp版
  6. 我和面试官的博弈:Redis 篇
  7. 奥本海姆信号与系统(第二版)笔记
  8. 【转载】Markdown编辑器如何在标题上添加序号
  9. js 鼠标放置图片展示大图并跟随鼠标移动
  10. IDA反汇编工具使用详解
  11. qq修改群名服务器失败,qq建群失败什么原因 q群一直创建失败 - 云骑士一键重装系统...
  12. 一种保留格式的加密算法FPE
  13. 论文阅读:A Novel Graph based Trajectory Predictor with Pseudo Oracle
  14. 女程序员放弃大厂年薪20万,回老家当实习老师,工作难找,后悔哭了
  15. linux slub分配器,Vi Linux内存 之 Slub分配器(六)
  16. HTML5简单个人主页设计
  17. 高德地图绘制标记点,点击弹出弹框进入第三方地图软件
  18. 经典算法应用之七----10亿数据中取最大的100个数据
  19. 16.深入浅出:电压比较器——参考《模拟电子技术基础》清华大学华成英主讲
  20. 用JavaFx开发一个小游戏

热门文章

  1. Google Colab 免费GPU 教程
  2. Java—多线程创建详解
  3. 用按钮控制歌单的上一曲和下一曲 0130 winform
  4. 演练 聚美Y品商品分类导航 1002 html
  5. mongodb数据库的安装 for windows版本 0916
  6. celery-02-安装与使用说明-for-redis
  7. html常用标签6-表单标签
  8. 【转】Linux的五个查找命令:find,locate,whereis,which,type
  9. C# 3.0 新特性
  10. C#中静态构造函数的一些理解