如何将此 date/time 转换为 Python 中的 yyyymmdd?
How can I convert this date/time to yyyymmdd in Python?
我得到了这个 1974-11-27T00:00:00
但我不知道你怎么称呼这种格式所以我在网上找不到任何关于如何制作这个 yyyymmdd
假设是字符串,可以直接替换-
s = '1974-11-27T00:00:00'
s = s[:10].replace('-','')
要将您拥有的内容转换为日期,您可以使用 python datetime
:
import datetime
this_date = '1974-11-27T00:00:00'
# transform the string you have to a datetime type
your_date = datetime.datetime.strptime(this_date, '%Y-%m-%dT%H:%M:%S')
# print it in the format you want to
print(your_date.strftime("%Y%m%d"))
19741127
这称为 ISO 格式的日期时间字符串。您只需执行以下操作即可将其转换为日期时间对象:
import datetime
example = '1974-11-27T00:00:00'
d = datetime.datetime.fromisoformat(example)
现在这是一个 date-time 对象,您可以根据需要对其进行格式化:
print(d.strftime('%Y%m%d'))
>> 19741127
试试这个:
import datetime
str = '1974-11-27T00:00:00'
datetime.strptime(str,"%Y-%m-%dT%H:%M:%S")
您拥有的这个日期采用标准 ISO 8601 格式。将其转换为 Python datetime
对象(假设您有字符串)运行
from datetime import datetime
s = '1974-11-27T00:00:00'
d = datetime.fromisoformat(s)
一旦它成为合适的 datetime
实例,您就可以随意格式化它
print(d.strftime("%Y%m%d"))
19741127
我得到了这个 1974-11-27T00:00:00
但我不知道你怎么称呼这种格式所以我在网上找不到任何关于如何制作这个 yyyymmdd
假设是字符串,可以直接替换-
s = '1974-11-27T00:00:00'
s = s[:10].replace('-','')
要将您拥有的内容转换为日期,您可以使用 python datetime
:
import datetime
this_date = '1974-11-27T00:00:00'
# transform the string you have to a datetime type
your_date = datetime.datetime.strptime(this_date, '%Y-%m-%dT%H:%M:%S')
# print it in the format you want to
print(your_date.strftime("%Y%m%d"))
19741127
这称为 ISO 格式的日期时间字符串。您只需执行以下操作即可将其转换为日期时间对象:
import datetime
example = '1974-11-27T00:00:00'
d = datetime.datetime.fromisoformat(example)
现在这是一个 date-time 对象,您可以根据需要对其进行格式化:
print(d.strftime('%Y%m%d'))
>> 19741127
试试这个:
import datetime
str = '1974-11-27T00:00:00'
datetime.strptime(str,"%Y-%m-%dT%H:%M:%S")
您拥有的这个日期采用标准 ISO 8601 格式。将其转换为 Python datetime
对象(假设您有字符串)运行
from datetime import datetime
s = '1974-11-27T00:00:00'
d = datetime.fromisoformat(s)
一旦它成为合适的 datetime
实例,您就可以随意格式化它
print(d.strftime("%Y%m%d"))
19741127