简体   繁体   English

将天数添加到 Python 中的 ISO 8601 格式日期

[英]Adding days to a ISO 8601 format date in Python

I need to add +3 hours to a date in iso 8601 format in python, for example "2022-09-21T22:31:59Z" due to time difference.由于时差,我需要将 +3 小时添加到 python 中 iso 8601 格式的日期,例如“2022-09-21T22:31:59Z”。 In this time information that is returned to me from the API, I only need Y/M/D information, but due to the +3 hour difference, the day information needs to go one step further in the date information, as will be experienced in the date I conveyed in the example.在这个从 API 返回给我的时间信息中,我只需要 Y/M/D 信息,但是由于 +3 小时的差异,日期信息需要 go 在日期信息中更进一步,如将经历在我在示例中传达的日期。 How can I overcome this problem?我该如何克服这个问题? I think the format of the date format is ISO 8601 but can you correct me if I am wrong?我认为日期格式的格式是 ISO 8601,但如果我错了,你能纠正我吗?

ex.前任。 api response; api 响应;

"createdDateTime": "2022-09-21T22:31:59Z"

what i need;我需要的;

"createdDateTime": "2022-09-21T22:31:59Z" to "2022-09-22T01:31:59Z"

Try this code it will definitely work:试试这个代码它肯定会工作:

from datetime import datetime,timedelta

parsed_date=datetime.strptime("2022-09-21T22:31:59Z", "%Y-%m-%dT%H:%M:%SZ")

Updated_date = parsed_date+ timedelta(hours=3)

print(Updated_date)

If you have a proper JSON string you can parse it with json , extract the string value, parse that with datetime.fromisoformat into a datetime value and then get the date from it:如果您有正确的 JSON 字符串,则可以使用json对其进行解析,提取字符串值,使用datetime.fromisoformat将其解析为datetime值,然后从中获取date

import json
from datetime import datetime
data=json.loads('{"createdDateTime": "2022-09-21T22:31:59+00:00"}')
isostr=data['createdDateTime'].replace('Z','+00:00')
fulldate=datetime.fromisoformat(isostr)
fulldate.date()
-----
datetime.date(2022, 9, 21)

The replacement is necessary because fromisoformat doesn't understand Z替换是必要的,因为fromisoformat不理解Z

Adding 3 hours to fulldate will return 1 AM in the next day:将 3 小时添加到fulldate将在第二天返回 1 AM:

fulldate + timedelta(hours=3)
------
datetime.datetime(2022, 9, 22, 1, 31, 59, tzinfo=datetime.timezone.utc)

fulldate is in UTC. fulldate是 UTC。 It can be converted to another timezone offset using astimezone可以使用astimezone将其转换为另一个时区偏移量

fulldate.astimezone(tz=timezone(timedelta(hours=3)))
---
datetime.datetime(2022, 9, 22, 1, 31, 59, tzinfo=datetime.timezone(datetime.timedelta(seconds=10800)))

Or in a more readable form:或者以更易读的形式:

fulldate.astimezone(tz=timezone(timedelta(hours=3))).isoformat()
---------------------------
'2022-09-22T01:31:59+03:00'

This is 1AM in the next day but with a +3:00 offset .这是第二天凌晨 1 点,但有 +3:00 偏移量 This is still the same time as 22PM at UTC.这仍然与 UTC 下午 22 点相同。

It's also possible to just replace the UTC offset with another one, without changing the time values, using replace :也可以使用replace将 UTC 偏移量替换为另一个,而不更改时间值:

fulldate.replace(tzinfo=timezone(timedelta(hours=3))).isoformat()
----------------------------
'2022-09-21T22:31:59+03:00'

That's the original time with a different offset.那是具有不同偏移量的原始时间。 That's no longer the same time as 2022-09-21T22:31:59Z这不再与2022-09-21T22:31:59Z相同

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

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