如何从datetime.fromisoformat中删除时间,如果没有提供时间?

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

How to remove time from datetime.fromisoformat if not given?

问题

date_str = "2020-01-01"
date_obj = datetime.fromisoformat(date_str)
# date_obj 的输出:2023-01-01

date_str = "2020-01-01T20"
date_obj = datetime.fromisoformat(date_str)
# date_obj 的输出:2023-01-01 20:00:00

date_str = "2020-01-01T00:00:00"
date_obj = datetime.fromisoformat(date_str)
# date_obj 的输出:2023-01-01 00:00:00
英文:

What do I need to do to get the following responses for the different input strings?

date_str = "2020-01-01"
date_obj = datetime.fromisoformat(date_str)
# Output for date_obj: 2023-01-01

date_str = "2020-01-01T20"
date_obj = datetime.fromisoformat(date_str)
# Output for date_obj: 2023-01-01 20:00:00

date_str = "2020-01-01T00:00:00"
date_obj = datetime.fromisoformat(date_str)
# Output for date_obj: 2023-01-01 00:00:00

At the moment, I always get the time included in the DateTime object. When no time is set, then it will include 00:00:00.

答案1

得分: 1

使用try/except
```py
from datetime import datetime, date

for date_str in "2020-01-01", "2020-01-01T20", "2020-01-01T00:00:00":
    try:
        print("日期 ->", date.fromisoformat(date_str))
    except ValueError:
        print("日期时间 ->", datetime.fromisoformat(date_str))
        
# 日期 ->  2020-01-01
# 日期时间 ->  2020-01-01 20:00:00
# 日期时间 ->  2020-01-01 00:00:00

或者检查日期/时间分隔符:

for date_str in "2020-01-01", "2020-01-01T20", "2020-01-01T00:00:00":
    if "T" in date_str or " " in date_str:
        print("日期时间 ->", datetime.fromisoformat(date_str))
    else:
        print("日期 ->", date.fromisoformat(date_str))
        
# 日期 ->  2020-01-01
# 日期时间 ->  2020-01-01 20:00:00
# 日期时间 ->  2020-01-01 00:00:00

<details>
<summary>英文:</summary>

using a try/except:
```py
from datetime import datetime, date

for date_str in &quot;2020-01-01&quot;, &quot;2020-01-01T20&quot;, &quot;2020-01-01T00:00:00&quot;:
    try:
        print(&quot;date -&gt; &quot;, date.fromisoformat(date_str))
    except ValueError:
        print(&quot;datetime --&gt; &quot;, datetime.fromisoformat(date_str))
        
# date -&gt;  2020-01-01
# datetime --&gt;  2020-01-01 20:00:00
# datetime --&gt;  2020-01-01 00:00:00

or a check for the date/time separator:

for date_str in &quot;2020-01-01&quot;, &quot;2020-01-01T20&quot;, &quot;2020-01-01T00:00:00&quot;:
    if &quot;T&quot; in date_str or &quot; &quot; in date_str:
        print(&quot;datetime --&gt; &quot;, datetime.fromisoformat(date_str))
    else:
        print(&quot;date --&gt; &quot;, date.fromisoformat(date_str))
        
# date --&gt;  2020-01-01
# datetime --&gt;  2020-01-01 20:00:00
# datetime --&gt;  2020-01-01 00:00:00

答案2

得分: 0

使用:

date_obj = datetime.fromisoformat(date_str).date()

这会得到:

2020-01-01
2020-01-01
2020-01-01
英文:

Use:

date_obj = datetime.fromisoformat(date_str).date()

which gives:

2020-01-01
2020-01-01
2020-01-01

huangapple
  • 本文由 发表于 2023年6月5日 18:23:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76405489.html
匿名

发表评论

匿名网友

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

确定