How do I get the Unix epoch in seconds for a date without time?

huangapple go评论86阅读模式
英文:

How do I get the Unix epoch in seconds for a date without time?

问题

在Go语言中,如何获取不带时间的日期的Unix纪元(以秒为单位)?

例如,如果我想要当前时间的Unix纪元秒数,我可以使用以下代码:

today := time.Now().Unix()

例如,如果当前的日期+时间是2021-12-31 11:56:01,我想要获取2021-12-31 00:00:00的Unix纪元秒数。是否有一种简单的方法来实现这个,还是我需要将其转换为字符串,操作字符串,然后解析字符串?

英文:

In Go, how do I get the Unix epoch in seconds for a date without time?

For example, if I want the Unix epoch seconds for the current time, I can use:

today := time.Now().Unix()

For instance, if the current date+time is 2021-12-31 11:56:01, I want to get the Unix epoch in seconds for 2021-12-31 00:00:00. Is there an easy way to do this, or do I have to convert to a string, manipulate the string, then parse the string?

答案1

得分: 3

你可以使用 time.Truncate 方法将时间舍入到整天:

today_00 := time.Now().Truncate(24*time.Hour).Unix()
英文:

You can use time.Truncate for rounding the time to the full day:

today_00 := time.Now().Truncate(24*time.Hour).Unix()

huangapple
  • 本文由 发表于 2021年12月31日 22:42:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/70543309.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定