简体   繁体   English

将一年中的某一天转换为日期

[英]Convert day of year to date

How do I convert day-of-year to date?如何将年份转换为日期? For example, what's the date for 104th day of 2004?例如,2004 年的第 104 天是什么日期?

This is the opposite of How do you convert POSIX date to day of year in R?这与如何将 POSIX 日期转换为 R 中的日期相反

I found that @TMS answer ignores the year specified in the origin, replacing it with the actual year you are executing the command (as @Shekeine found).我发现@TMS答案忽略了原点中指定的年份,将其替换为您执行命令的实际年份(如@Shekeine 发现的那样)。 Doing this seems to work fine though:这样做似乎工作正常:

as.Date(104, origin = "2014-01-01")

CAUTION: day-of-year is zero based in this case!注意:在这种情况下,年的日期为零!

This is the way I do it:这是我这样做的方式:

as.Date(104, format = "%j", origin = "1.1.2014")
# "2014-04-15"

PS: for those who wonder if answering my own question is encouraged, please look here: https://meta.stackexchange.com/questions/17463/can-i-answer-my-own-questions-even-if-i-knew-the-answer-before-asking PS:对于那些想知道是否鼓励回答我自己的问题的人,请看这里: https : //meta.stackexchange.com/questions/17463/can-i-answer-my-own-questions-even-if-i-先问后知

as.Date(2, origin = '2014-01-01')

As for the issue with above code giving the output as 2014-01-03 .至于上面代码的问题,输出为2014-01-03

You can just put the origin as the last day of the previous year.您可以将原点作为上一年的最后一天。

as.Date(2, origin = '2013-12-31')

It works that way.它是这样工作的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM