網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
golang中并沒(méi)有像java一樣提供類(lèi)似yyyy-MM-dd HH:mm:ss
格式的操作,而是將其定義為golang的誕生時(shí)間:
2006-01-02 15:04:05 -0700 MST
注意這在golang的日期格式化里不是一個(gè)具體日期,而是格式,這樣如果我們需要格式化日期,可以如下操作
timeNow := time.Now() fmt.Println("yyyy-MM-dd HH:mm:ss" ,timeNow.Format("2006-01-02 15:04:05")) // 打印: yyyy-MM-dd HH:mm:ss: 2022-05-11 14:24:19
接下來(lái)說(shuō)下Time
中的幾個(gè)方法:
Time.Unix() 獲取時(shí)間秒數(shù),返回的是unix時(shí)間秒
fmt.Println("from 1970-01-01 to now seconds: ",int32(time.Now().Unix()))
Time.Date() 返回年月日:
year,month,day := timeNow.Date() fmt.Println(year,month,day) // 注意打印的是: 2022 May 11
Time.AddDate 時(shí)間增減:
timeLast := timeNow.AddDate(0,0,-1) fmt.Println(timeLast.Format("2006-01-02 15:04:05"))
Time.Add 增加指定的時(shí)間,可以精確到納秒
time2 := timeNow.Add(time.Duration(2000*time.Millisecond)) fmt.Println(time2.Format("2006-01-02 15:04:05"))
Time.After 是否在指定時(shí)間之后,如果是返回true Time.Before 是否在指定時(shí)間之前,如果是返回true time.Parse 按照指定格式將字符串轉(zhuǎn)換為日期:
time2,err := time.Parse("2006-01-02 15:04:05","2022-05-11 15:04:05") if err != nil { fmt.Println(err) } fmt.Println(time2.Format("2006-01-02 15:04:05"))
補(bǔ)充:go語(yǔ)言時(shí)間和日期的轉(zhuǎn)化
go 語(yǔ)言中使用time.Time表示時(shí)間。我們可以通過(guò)time.Now函數(shù)獲取當(dāng)前的時(shí)間對(duì)象,然后從時(shí)間對(duì)象中可以獲取到年、月、日、時(shí)、分、秒等信息。
package main import ( ?? ?"fmt" ?? ?"time" ) func main() { ?? ?t := time.Now() // 獲取當(dāng)前時(shí)間 ?? ?fmt.Printf("當(dāng)前時(shí)間:%v\n", t) ?? ?fmt.Println("年", t.Year()) ?? ?fmt.Println("月", t.Month()) ?? ?fmt.Println("日", t.Day()) ?? ?fmt.Println("時(shí)", t.Hour()) ?? ?fmt.Println("分", t.Minute()) ?? ?fmt.Println("秒", t.Second()) }
輸出
當(dāng)前時(shí)間:2022-05-19 21:38:39.7367663 +0800 CST m=+0.001805001
年 2022
月 May?
日 19 ?
時(shí) 21 ?
分 38 ?
time 包中定義的時(shí)間間隔類(lèi)型的常量如下:
const ( ? ? Nanosecond ?Duration = 1 ? ? Microsecond ? ? ? ? ?= 1000 * Nanosecond ? ? Millisecond ? ? ? ? ?= 1000 * Microsecond ? ? Second ? ? ? ? ? ? ? = 1000 * Millisecond ? ? Minute ? ? ? ? ? ? ? = 60 * Second ? ? Hour ? ? ? ? ? ? ? ? = 60 * Minute )
例如:time.Hour表示1小時(shí),time.Second表示1秒。
總結(jié)
原文鏈接:https://blog.csdn.net/LeoHan163/article/details/124710250
相關(guān)推薦
- 2023-07-16 oracle 創(chuàng)建定時(shí)任務(wù)
- 2022-05-26 Python借助with語(yǔ)句實(shí)現(xiàn)代碼段只執(zhí)行有限次_python
- 2023-04-06 Pytorch中關(guān)于model.eval()的作用及分析_python
- 2022-11-11 C#中ArrayList?類(lèi)的使用詳解_C#教程
- 2022-05-23 詳解Rust中的workspace_相關(guān)技巧
- 2022-07-31 如何理解C++指針常量和常量指針_C 語(yǔ)言
- 2022-04-25 利用Python寫(xiě)個(gè)摸魚(yú)監(jiān)控進(jìn)程_python
- 2022-12-23 C++?Boost?Parameter超詳細(xì)講解_C 語(yǔ)言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支