当前位置:网站首页>Golang-- operation date
Golang-- operation date
2022-07-21 06:48:00 【It artist rookie】
The time interval
const (
Nanosecond Duration = 1
Microsecond = 1000 * Nanosecond
Millisecond = 1000 * Microsecond
Second = 1000 * Millisecond
Minute = 60 * Second
Hour = 60 * Minute
)
now := time.Now() // Get the current time
timestamp := now.Unix() // Second timestamps 1658223718
milli := now.UnixMilli() // Millisecond time stamp Go1.17+ 1658223718098
micro := now.UnixMicro() // Microsecond timestamps Go1.17+ 1658223718098170
nano := now.UnixNano() // Nanosecond time stamp 1658223718098170800
fmt.Println(timestamp, milli, micro, nano)
The time zone
// To create a time object, you need to specify a location .
// Common locations are time.Local( Local time ) and time.UTC(UTC Time ).
//timeInLocal := time.Date(2009, 1, 1, 20, 0, 0, 0, time.Local) // System local time
// Beijing time. ( East eight ) Than UTC Good morning! 8 Hours
// timezoneDemo Time zone example
func timezoneDemo() {
// There is no daylight saving time in China , Use a fixed 8 Hours of UTC time difference .
// For many other countries, summer time needs to be considered .
secondsEastOfUTC := int((8 * time.Hour).Seconds())
// FixedZone Return always use the given area name and offset (UTC East Second ) Of Location.
beijing := time.FixedZone("Beijing Time", secondsEastOfUTC)
// If the current system time zone database , You can load a location to get the corresponding time zone
// for example , Load the time zone of New York
newYork, err := time.LoadLocation("America/New_York") // UTC-05:00
if err != nil {
fmt.Println("load America/New_York location failed", err)
return
}
fmt.Println()
// Load the time zone of Shanghai
//shanghai, err := time.LoadLocation("Asia/Shanghai") // UTC+08:00
// Load the time zone of Tokyo
//tokyo, err := time.LoadLocation("Asia/Tokyo") // UTC+09:00
// To create a time object, you need to specify a location . Common locations are time.Local( Local time ) and time.UTC(UTC Time ).
//timeInLocal := time.Date(2009, 1, 1, 20, 0, 0, 0, time.Local) // System local time
timeInUTC := time.Date(2009, 1, 1, 12, 0, 0, 0, time.UTC)
sameTimeInBeijing := time.Date(2009, 1, 1, 20, 0, 0, 0, beijing)
sameTimeInNewYork := time.Date(2009, 1, 1, 7, 0, 0, 0, newYork)
// Beijing time. ( East eight ) Than UTC Good morning! 8 Hours , So the above two times seem to be different 8 Hours , But it means the same time
timesAreEqual := timeInUTC.Equal(sameTimeInBeijing)
fmt.Println(timesAreEqual)
// New York ( West five ) Than UTC On the evening of 5 Hours , So the above two times seem to be different 5 Hours , But it means the same time
timesAreEqual = timeInUTC.Equal(sameTimeInNewYork)
fmt.Println(timesAreEqual)
}
var timeLayout = "2006-01-02 15:04:05"
Remove the time part from the string date ( That is, hours, minutes and seconds )
func DateStringWipeOffTime(date string) string {
date = strings.TrimPrefix(date, " ")
date = strings.TrimSuffix(date, " ")
if strings.Contains(date, " ") {
i := strings.Index(date, " ")
date = date[:i]
return date
}
return date
}
Add the time part to the string date ( Default part )
func DateStringAddTime(date string) string {
date = strings.TrimPrefix(date, " ")
date = strings.TrimSuffix(date, " ")
if strings.Contains(date, " ") {
return date
}
date = date + " 00:00:00"
return date
}
Get the local time according to the string date (int64 type )
func StringToLocationInt64(date string) int64 {
loc, _ := time.LoadLocation("Local")
if strings.Contains(date, " ") {
dateTime, _ := time.ParseInLocation(timeLayout, date, loc)
dateTimeInt := dateTime.Unix()
return dateTimeInt
}
dateTime, _ := time.ParseInLocation("2006-01-02", date, loc)
dateTimeInt := dateTime.Unix()
return dateTimeInt
}
String date to time.Time Don't be part of the hour, minute and second
func StringToDateWithOutTime(date string) string {
dateTimeInt := StringToLocationInt64(date)
result := time.Unix(dateTimeInt, 0).Format("2006-01-02")
return result
}
String date to time.Time, Keep the part of hours, minutes and seconds
func StringToDateWithInTime(date string) string {
dateTimeInt := StringToLocationInt64(date)
result := time.Unix(dateTimeInt, 0).Format(timeLayout)
return result
}
Date calculation , Increase the specified number of days , Do not keep hours, minutes and seconds
func DateAddDayWhitOutTime(date string, days int) string {
dateTimeInt := int(StringToLocationInt64(date))
oneDay := 86400
addingDays := oneDay * days
dateTimeInt += addingDays
dateTimeString := time.Unix(int64(dateTimeInt), 0).Format("2006-01-02")
return dateTimeString
}
Date calculation , Increase the specified number of days , Reserved hours, minutes and seconds
func DateAddDayWhitInTime(date string, days int) string {
dateTimeInt := int(StringToLocationInt64(date))
oneDay := 86400
addingDays := oneDay * days
dateTimeInt += addingDays
dateTimeString := time.Unix(int64(dateTimeInt), 0).Format("2006-01-02 15:04:05")
return dateTimeString
}
Timer
func tickDemo() {
ticker := time.Tick(time.Second) // Define a 1 Second interval timer
for i := range ticker {
fmt.Println(i)// Tasks that are performed every second
}
}
边栏推荐
- AWD思路
- 你必须知道的4种 Redis 集群方案及优缺点对比
- Jenkins pipeline downloads the code to the specified workspace
- Section 12 of Chapter 1: use of break and continue
- Google 为造芯再掀“抢人大战”,英特尔 17 年老将加入!
- Circular linked list of leetcode simple problem
- 第一章 第六节:变量
- Function introduction
- DDR 6 内存已经投入研发
- PostgreSQL是什么?StackOverflow上开发者最爱和最想要的关系型数据库
猜你喜欢
随机推荐
着力提升妇儿获得感、幸福感、安全感!未来十年广州妇女儿童事业这样发力
(Note)神经网络中的特征融合方式(add/concate)
Section 7 of Chapter 1: constants
多线程与高并发day08
在 Business Application Studio 里使用 SAP UI5 应用消费 OData 的 Create 和 Delete 操作
深入理解String类
Mysql、Oracle、PostgreSql数据库索引失效场景详细讲解
[cloud resident co creation] full scene software development production line, end-to-end efficiency improvement, full link security
DDR 6 内存已经投入研发
用 emoji 学安全上网小常识?看 Google 新玩法
亮点抢先看!2022 开放原子全球开源峰会定于 7 月 25-29 日在北京举办
Multithreading and high concurrency day08
算力网络,AI先行,昇腾AI助力运营商数字化转型 ——携手聚力,共赢算力时代
Section 1 of Chapter 2: detailed explanation of basic data types
When does MySQL use table locks and row locks?
Nacos服务发现数据模型
“如今,99.9% 以上的代码都是垃圾!”
认清影响因子引发的是是非非,善待逆境中顽强崛起的国产期刊
Flink系列之Flink内存模型:从宏观(Flink内存模型)、微观(Flink内存结构)、数据传输等角度分析Flink的内存管理
MySQL数据库执行SQL查询语句时,底层实现原理(超详细)