简体   繁体   English

以日期时间格式转换字符串

[英]Convert string in format datetime

I have this date that comes to me in the following format and it is string type我有以下格式的日期,它是字符串类型

from datetime import datetime 

fecha_str = "2021-09-27T20:42:34.099000Z"

fecha_datetime = datetime.strptime(fecha_str,'%d del %m de %Y a las %H:%M')

I need to transform it into a datetime type varibale so that I can manipulate it and only show the information that is needed.我需要将其转换为日期时间类型变量,以便我可以对其进行操作并仅显示所需的信息。

The format string you're passing to strptime() is for an output (ie, used with strftime() ).您传递给strptime()的格式字符串适用于 output(即与strftime()一起使用)。 The timestamp you are parsing is in UTC (the trailing 'Z'), and ISO8601 format with milliseconds.您正在解析的时间戳采用 UTC(尾随“Z”)和带毫秒的 ISO8601 格式。

>>> fecha_str = "2021-09-27T20:42:34.099000Z"
>>> fecha_datetime = datetime.strptime(fecha_str, "%Y-%m-%dT%H:%M:%S.%fZ")

>>> fecha_datetime
datetime.datetime(2021, 9, 27, 20, 42, 34, 99000)

from datetime import datetime从日期时间导入日期时间

date_time_str = '18/09/19 01:55:19' date_time_str = '18/09/19 01:55:19'

date_time_obj = datetime.strptime(date_time_str, '%d/%m/%y %H:%M:%S') date_time_obj = datetime.strptime(date_time_str, '%d/%m/%y %H:%M:%S')

print ("The type of the date is now", type(date_time_obj)) print ("The date is", date_time_obj) print ("日期的类型是现在", type(date_time_obj)) print ("日期是", date_time_obj)

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

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