当前位置:网站首页>golang 时间格式化
golang 时间格式化
2022-04-22 10:08:00 【昵称你猜8】
科普 CST 含义
CST: 中部标准时间 (Central Standard Time) 同时表示下面4个时区
CST Central Standard Time(USA) UT-6:00 CST Central Standard Time(Australia) UT 9:30 CST China Standard Time UT 8:00 CST Cuba Standard Time UT-4:0
我们常遇到的应该就是 China Standard Time.
GMT 和 UTC 的关系
UTC (Universal Time Coordinated), 以子午初线(经度0)上的评价太阳时为依据, 也就是英国伦敦的平均太阳时
GMT (Greenwitch Mean Time) 格林威治平均时间, 由于地球绕太阳的运行的轨道是椭圆, 导致 UTC 表示的时间, 不是很准确, 因此提出了 GMT 时间, 每年或者2年对 UTC 增加一个闰秒, 来完成修正. 一般上我们可以认为 GMT 和 UTC 是一样的.
golang 的时间 认准 "2006-1-2 15:04:05.000 PM Mon Jan GMT " 去格式化就可以了。也就是1234567,分别对应:月日时分秒年 时差,很好记忆。只是稍微注意一下:
- 月:01或Jan都可以
- 小时:03表示12小时制,15表示24小时制。
- 时差:是 -07 ,不是 07,后边可以增加“00”或“:00”,表示更进一步的分秒时差。
- 上下午:使用PM,不是AM。
- 摆放顺序:随意,甚至重复都可以。源代码包也有定义的常用格式供使用。
package main
import (
"fmt"
"time"
)
func main() {
timeNow := time.Now()
fmt.Println(timeNow.Format("2006-01-02 15:04:05")) // 2022-4-18 14:20:45
fmt.Println(timeNow.Format("2006/01/02")) // 2021/06/25
// 获取当前时间戳
fmt.Println(time.Now().Unix())
// 指定的时间进行格式化
fmt.Println(time.Unix(1650263305, 0).Format("2006-01-02 15:04:05"))
//24小时之内的时间计算 ParseDuration
//1小时1分1s之前
t1, _ := timeNow.ParseDuration("-1h1m1s")
fmt.Println(t1)
m1 := curtime.Add(t1)
fmt.Println(m1)
//24小时之外的时间计算
beforeDay := timeNow.AddDate(0, 0, -1) // 三个参数分别是年月日,此处获取的是前一天的日期
beforeMonth := timeNow.AddDate(0, -1, 0) // 前一个月的日期
beforeYear := timeNow.AddDate(-1, 0, 0) // 去年的当天日期
fmt.Println(beforeDay)
fmt.Println(beforeMonth)
fmt.Println(beforeYear)
fmt.Println(beforeDay.Format("2006-01-02 15:04:05"))
//24小时
fmt.Println(timeNow.Format("2006-1-2 15:04:05.000 PM Mon Jan")) // 2021-6-25 10:59:05.410 AM Fri Jun
fmt.Println(timeNow.Format("2006/01/02 15:04")) // 2021/06/25 10:59
fmt.Println(timeNow.Format("2006-1-2 15:04:05.000")) // 2021-6-25 10:59:05.410
fmt.Println(timeNow.Format("Mon, 02 Jan 2006 15:04:05 GMT")) // Fri, 25 Jun 2021 10:59:05 GMT
// 12小时制
fmt.Println(timeNow.Format("2006-01-02 03:04:05.000 PM Mon Jan")) // 2021-06-25 10:59:05.410 AM Fri Jun
fmt.Println(timeNow.Format("15:04 2006/01/02")) // 10:59 2021/06/25
}
版权声明
本文为[昵称你猜8]所创,转载请带上原文链接,感谢
https://blog.csdn.net/hudeyong926/article/details/124248713
边栏推荐
- js老生常谈之this,constructor ,prototype
- SQL relational database management system
- MySQL multi instance installation method I
- linux7静默安装oracle12c报错[FATAL] [INS-35344]The value is not specified
- Deep learning remote sensing scene classification data set sorting
- L2-033 简单计算器 (25 分)
- 中国电信CTyunOS规模部署,携手openEuler共筑天翼云数字底座
- Ctyunos scale deployment of China Telecom and jointly build Tianyi cloud digital base with openeuler
- Linux Installation Oracle 19C Full Version (graphics + silent installation)
- 项目实训-读报僵尸
猜你喜欢

Sorting and publishing of remote sensing image segmentation data set

source_ Insight is a method to create a new project and search for a variable or function in the whole document

【Go】程序流程控制

Unity3D build时错误提示:Missing Project ID in Unity 解决方案

CLA: silver bullet to reduce the compliance risk of open source community?

Softing datafeed OPC Suite: empowering industrial equipment to connect to the Internet of things

辐射度量学基本概念

leapftp:425 failed to establish connection解决方法

The VirtualBox virtual machine uses virtio net to run dpdk + VPP and cannot receive VLAN messages

stringstream的用法
随机推荐
JWT源码浅析(四层封装,有示意图)
Various location codes applied in transformer model
Depth first search (I): middle order traversal of binary tree (force buckle)
Command ‘yum‘ not found, but can be installed with: apt install yum
5.嵌入式控制器(Embedded Controller,EC)学习 PS/2协议
leapftp:425 failed to establish connection解决方法
Unity3D build时错误提示:Missing Project ID in Unity 解决方案
SQL 操作符
Oracle 11.0 under Windows Upgrade from 2.0.1 to Oracle 11.0 2.0.4
The working principle of triode, FET and MOS transistor
Cross platform compilation
用MySQL创建学生管理系统
Secondary encapsulation of requests for uni app projects
source_insight新建工程以及在整个文档中搜索某个变量或者函数的方法
openEuler Kernel 技术解读 | 内核中断子系统介绍
SQL 语法
CLA:减少开源社区合规风险的银弹?
三极管、场效应管和MOS管三者的工作原理
最通俗易懂的依赖注入之生命周期
Aardio - [library] webp picture conversion