英文:
Calculate year and julian date from a value that is n days since start date
问题
我有一个时间序列数据集,需要转换日期格式。目前的 'time' 值表示自 01011961(ddmmyyyy)以来的天数。我需要将其转换为两个值,一个表示年份,另一个表示儒略日期。
假设数据集中的 'time' 值为 x,我尝试了下面的公式(这是用 Python 编写的,但代码不如公式重要)。然而,由于闰年的存在,得到的日期不准确:
year = [int(1961 + (x/365)) for x in time]
date = [int((x) - (((year[0]-1961)*365)+9)) for x in time] # 加上 +9 是为了补偿之前的闰年,但不太准确
英文:
I have a time series dataset and I need to convert the date format. Currently the 'time' value represents the number of days since 01011961 (ddmmyyyy). I need to convert this to two values, one for the year and the other for the julian date.
Assuming the 'time' value in the dataset is x, I have tried the formula below(this is in python but the code is less important than the formula). However, the resulting dates are off because they are skewed by leap years:
year = [int(1961 + (x/365)) for x in time]
date = [int((x) - (((year[0]-1961)*365)+9))for x in time] #the +9 was to account for previous leap years but it's not quite right
答案1
得分: 1
不要尝试自己计算它。您可以使用datetime
模块来执行此操作:
英文:
Don't try to compute it yourself. You can do this with the datetime
module:
xxxxx:~/src$ python
Python 3.10.6 (main, May 29 2023, 11:10:38) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> epoch = datetime.date(1969,1,1)
>>> d=epoch+datetime.timedelta(days=12345)
>>> d
datetime.date(2002, 10, 20)
>>> d.timetuple().tm_yday
293
>>> d.timetuple().tm_year
2002
>>>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论