英文:
How to get a date in format "yyyy-mm-dd" from the day of year number and year in Golang?
问题
我正在尝试使用一年中的日期编号(0到365)作为输入,将日期格式化为"yyyy-mm-dd"的形式。
输入:035和2021
输出:2021-02-04
我真的不知道Go语言的时间库是否可以简单地完成这个任务。
有人知道如何做吗?
英文:
I am trying to format a date in the format "yyyy-mm-dd" using as input the number of the day in year (0 to 365).
As input : 035 and 2021
Output : 2021-02-04
I really don't know if the Go's time library can help doing this simply.
Anyone have any idea of to do it ?
答案1
得分: 3
如果你指的是*day number*,那通常是从1到365(闰年为366)。因为只有这样,2021年的第35天对应于2021-02-04。
只需使用time.Date
,它会正确地转换超出范围的day
参数:
> 月份、日期、小时、分钟、秒和纳秒的值可能超出其通常范围,并在转换过程中进行归一化。例如,10月32日转换为11月1日。
示例:
package main
import (
"fmt"
"time"
)
func main() {
year, day := 2021, 35
dt := time.Date(year, 1, day, 0, 0, 0, 0, time.Local)
fmt.Println(dt.Format("2006-01-02"))
}
输出:
2021-02-04
如果你实际上指的是从0到364(闰年为365)的天数,那么将day
更改为day+1
,像这样:
dt := time.Date(year, 1, day+1, 0, 0, 0, 0, time.Local)
英文:
If you mean day number, then that usually goes from 1 to 365 (or 366 for leap year). Because only then day 35 of 2021 corresponds to 2021-02-04.
Just use time.Date
, it will convert an out-of-range day
parameter correctly:
> The month, day, hour, min, sec, and nsec values may be outside their usual ranges and will be normalized during the conversion. For example, October 32 converts to November 1.
Example:
package main
import (
"fmt"
"time"
)
func main() {
year, day := 2021, 35
dt := time.Date(year, 1, day, 0, 0, 0, 0, time.Local)
fmt.Println(dt.Format("2006-01-02"))
}
Prints:
2021-02-04
If you actually mean day number 0 to 364 (or 365 for leap year), then change day
to day+1
like so:
dt := time.Date(year, 1, day+1, 0, 0, 0, 0, time.Local)
答案2
得分: 1
这是代码:
package main
import (
"fmt"
"time"
)
func dayOfYear(year, days int) time.Time {
return time.Date(year, 1, 1+days, 0, 0, 0, 0, time.UTC)
}
func main() {
t := dayOfYear(2021, 35)
fmt.Println(t)
}
不过,你的计算有误,因为35天应该是2月5日,因为一年的开始是1月1日,而不是1月0日。
https://golang.org/pkg/time#Date
英文:
This does it:
package main
import (
"fmt"
"time"
)
func dayOfYear(year, days int) time.Time {
return time.Date(year, 1, 1+days, 0, 0, 0, 0, time.UTC)
}
func main() {
t := dayOfYear(2021, 35)
fmt.Println(t)
}
Although, your math is off, as 35 days would be February 5, as the year starts
January 1, not January 0.
答案3
得分: -1
这可能是你要找的内容,请确保用你需要显示时间字符串的时区替换掉代码中的时区。
package main
import (
"fmt"
"time"
)
func main() {
var layout = "2006-01-02"
zone := time.UTC
year := 2021
day := 35
time := time.Date(year, 1, day, 0, 0, 0, 0, zone)
converted := time.Format(layout)
fmt.Println(converted)
}
请注意,这是一个Go语言的代码示例,用于将指定日期转换为特定格式的时间字符串。你可以根据需要修改代码中的年份、日期和时区来获取你想要的结果。
英文:
This might be what you are looking for, make sure to replace the zone with the zone in which you need the time string to be displayed.
package main
import (
"fmt"
"time"
)
func main() {
var layout = "2006-01-02"
zone := time.UTC
year := 2021
day := 35
time := time.Date(year,1,day,0,0,0,0,zone)
converted := time.Format(layout)
fmt.Println(converted)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论