该文可以快速在Go语言中获得时间的计算。

在Go中获取时间

如何获取当前时间

now := time.Now()
fmt.Printf("current time is :%s", now) current time is :2009-11-10 23:00:00 +0000 UTC m=+0.000000001

如何获取UNIX Timestamp

cur_time := time.Now().Unix()
fmt.Printf("current unix timestamp is :%v\n", cur_time )

如何获取当日0:00:00 0:00:00

now := time.Now()
date := time.Date(now.Year(), now.Month(), now.Day(),0, 0, 0, 0, time.Local)
fmt.Printf("date is :%s", date) date is :2021-04-13 00:00:00 +0800

如何获取时区时间

标准时间 time.Now().UTC()

本地时区 time.Now().Local()


// 获取0时区时间
fmt.Printf("date is :%s\n", time.Now().UTC()) date is :2021-04-13 16:02:33.853254 +0000 UTC // 快速设置时区
timeLocation, _ := time.LoadLocation("Asia/Tokyo") //使用时区码
fmt.Println(time.Now().In(timeLocation).String()) // 快速设置时区 2021-04-14 01:09:18.140997 +0900 JST

Go中的固定时间格式

获取月份

time.April

type Month int

const (
January Month = 1 + iota
February
March
April
May
June
July
August
September
October
November
December
)

获取星期

time.Sunday

type Weekday int

const (
Sunday Weekday = iota
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
)

Go中的时间格式化

Go中时间格式化的格式为 2006-01-02 15:04:05 612345为格式,而不是具体时间


// YYYY-MM-DD
fmt.Println(time.Now().Format("2006-01-02")) // YYYY-MM-DD hh:mm:ss
fmt.Println(time.Now().Format("2006-01-02 15:04:05")) // M-DD
fmt.Println(time.Now().Format("1-02")) // MM-DD
fmt.Println(time.Now().Format("01-02") // 获取当前的小时、分钟、秒(整数)
nowHour, nowMinute, nowSecond = time.Now().Clock() // 获取前一天
// AddDate(Years, months, days)
yesterday = time.Now().AddDate(0,0,-1).Format("01/02") // 显示星期英文简写
fmt.Println(time.Now().Format("2006-01-02 15:04:05 Mon")) // 星期的大写
fmt.Println(time.Now().Format("2006-01-02 15:04:05 Monday")) // 增加微秒
fmt.Println(time.Now().Format("2006-01-02 15:04:05.000000")) // 纳秒
fmt.Println(time.Now().Format("2006-01-02 15:04:05.000000000"))
} // print result
08-10-2018
08-10-2018 21:11:58
08-10-2018 21:11:58 Fri
08-10-2018 21:11:58 Friday
08-10-2018 21:11:58.880934
08-10-2018 21:11:58.880934320

Go中的时间计算

如何获取本周日期有哪些?

获取一个星期的第一天是几号

t:=time.Now()
fmt.Println(t.Weekday()) // 获取现在时间为本周的星期几

得到本日为星期几后,可以对时间进行计算,因为time包内星期的常量都为int,可以直接进行算数运算.

用一周的第一天减去当日为星期几,如果为0既『本日为本周的第一天』

time.AddDate(year, month, date),仅可以添加年月日

time.Add(Hours, Minutes, Seconds),仅可以添加时分秒

offset := int(time.Monday - t.Weekday()) //=-1

如不为0,time包提供了,「以当前时间为基点,进行加减运算」

// t.AddDate(year, month, date)
t.AddDate(0,0,offset) // 可以获取到,周一为几月几日

综上所属,可以获得每周第一天为几月几日,每周随后一天为几月几日


/**
* 获取上周周第一天具体年月日
**/ func GetLastWeekFirstDate() (weekMonday string) {
thisWeekMonday := GetFirstDateOfWeek()
TimeMonday, _ := time.Parse("2006-01-02", thisWeekMonday)
lastWeekMonday := TimeMonday.AddDate(0, 0, -7)
weekMonday = lastWeekMonday.Format("2006-01-02")
return
} /**
* 获取本周的周一具体年月日
**/ func GetFirstDateOfWeek() (weekMonday string) {
now := time.Now()
offset := int(time.Monday - now.Weekday())
if offset > 0 {
offset = -6
} weekStartDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
weekMonday = weekStartDate.Format("2006-01-02")
return
} /**
* 获取上周最后一天具体年月日
**/ func GetLastWeekLastDate() (weekMonday string) {
now := time.Now() offset := int(time.Monday - now.Weekday())
if offset > 0 {
offset = -6
} weekStartDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
weekMonday = weekStartDate.AddDate(0, 0, -1).Format("2006-01-02")
return
} /**
* 获取上周一星期所有天数的具体年月日
**/ func GetBetweenDates(sdate, edate string) []string {
d := []string{}
timeFormatTpl := "2006-01-02 15:04:05"
if len(timeFormatTpl) != len(sdate) {
timeFormatTpl = timeFormatTpl[0:len(sdate)]
}
date, err := time.Parse(timeFormatTpl, sdate)
if err != nil {
return d
}
date2, err := time.Parse(timeFormatTpl, edate)
if err != nil {
return d
}
if date2.Before(date) {
return d
}
// 输出日期格式固定
timeFormatTpl = "2006-01-02"
date2Str := date2.Format(timeFormatTpl)
d = append(d, date.Format(timeFormatTpl))
for {
date = date.AddDate(0, 0, 1)
dateStr := date.Format(timeFormatTpl)
d = append(d, dateStr)
if dateStr == date2Str {
break
}
}
return d
}

最全的go语言的时间格式的更多相关文章

  1. cmd提取时间格式(小时)问题以及Windows系统语言判断

    你在这里看到了我的现在的时间是01:15,没错正在做个开发,本来好好的,结果一运行,直接报错: 这里就是时间中的获取小时出了问题,之前23点那会已经调试通过了,过那时是没有问题的,那么这时发生了什么? ...

  2. pandas修改全列的时间格式 无需使用apply

    df.date.dt.strftime('%Y%m%d') #实现全列修改时间格式

  3. Windows 2012 英文版系统安装中文语言包及时间格式设置

    1.安装中文语言包:在运行窗口中输入"LPKSetup.exe",选择中文语言包安装.--------------------------------------------- 2 ...

  4. iOS- NSDateFormatter (自定义时间格式)

    一. NSDateFormatter解释 1. 日期(NSDate)是NSString类的格式(stringWithFormat),也可以改变输出,如果需要输出年代信息等则需要进行转换,等等. 2.  ...

  5. db2 日期时间格式

    db2日期和时间常用汇总 1.db2可以通过SYSIBM.SYSDUMMY1.SYSIBM.DUAL获取寄存器中的值,也可以通过VALUES关键字获取寄存器中的值. SELECT 'HELLO DB2 ...

  6. WinServer2008r2 机器时间格式修改

    windows2008 这么高级的系统不可能改个系统的日期时间显示格式还要进注册表啊.于是有baidu,google了下终于发现了,原来还有不需要注册表的更简便方法.windows2008默认时间格式 ...

  7. Win2008 IIS7日期时间格式更改最简便方法

    windows2008 这么高级的系统不可能改个系统的日期时间显示格式还要进注册表啊.于是有baidu,google了下终于发现了,原来还有不需要注册表的更简便方法. windows2008默认时间格 ...

  8. Sql日期时间格式转换;取年 月 日,函数:DateName()、DATEPART()

    一.sql server2000中使用convert来取得datetime数据类型样式(全) 日期数据格式的处理,两个示例: CONVERT(varchar(16), 时间一, 20) 结果:2007 ...

  9. sql 日期时间格式转换

    Sql日期时间格式转换   sql server2000中使用convert来取得datetime数据类型样式(全) 日期数据格式的处理,两个示例: CONVERT(varchar(16), 时间一, ...

随机推荐

  1. Maven项目中resources配置总结

    目录 背景 第一部分 基本配置介绍 第二部分 具体配置和注意事项 第三部分 读取resources资源 参考文献及资料 背景 通常Maven项目的文件目录结构如下: # Maven项目的标准目录结构 ...

  2. QT项目-Chart Themes Example学习(一)

    1.main.cpp #include "themewidget.h" #include <QtWidgets/QApplication> #include <Q ...

  3. 解决wampserver 服务无法启动

    如图左击选中apache的httpd.conf把文本中的80端口,改成未被占用的端口.

  4. mvel 配合正则表达式实现文本替换

    mvel 依赖 <dependency> <groupId>org.mvel</groupId> <artifactId>mvel2</artif ...

  5. 【Prolog - 2.0 基础应用】

    [术语统一 terms unify] 两者统一,只需满足下面两条件之一 1.原本就是相同的 2.包含变量,这些变量可以用术语统一实例化,从而得到相等的术语 mia和mia是统一的,42和42是统一的, ...

  6. pwnable.tw orw

    orw 首先,检查一下程序的保护机制 开启了canary保护,还是个32位的程序,应该是个简单的题

  7. SQL Server CDC配合Kafka Connect监听数据变化

    写在前面 好久没更新Blog了,从CRUD Boy转型大数据开发,拉宽了不少的知识面,从今年年初开始筹备.组建.招兵买马,到现在稳定开搞中,期间踏过无数的火坑,也许除了这篇还很写上三四篇. 进入主题, ...

  8. M1 和 Docker 谈了个恋爱

    出于开源项目的需要,我准备把之前在 windows 下运行的开源项目移植到 Mac 上跑得试下,但是 Mac M1 芯片并不能很好地支持 Docker,这不,发现 Docker 也正式支持 Mac 了 ...

  9. 基于MVC框架的JavaWeb网站开发demo项目(JSP+Servlet+JavaBean)

    1.环境配置 Windows10+Eclipse2020+jdk8+Tomcat9+MySQL8+Navicat10 2.需求分析 ①用户登录注册注销(查找.增加) ②显示用户列表(查找) ③显示用户 ...

  10. 过 DNF TP 驱动保护(一)

    过 DNF TP 驱动保护(一)   文章目录:                   01. 博文简介: 02. 环境及工具准备: 03. 分析 TP 所做的保护: 04. 干掉 NtOpenProc ...