简体   繁体   English

如何将字符串 2021-09-30_1 转换为日期时间

[英]How to convert string 2021-09-30_1 to datetime

How can I convert string 2021-09-30_1 to datetime 2021/09/30 00:00, which means that from the last string we have to substract one to get the hour.如何将字符串 2021-09-30_1 转换为日期时间 2021/09/30 00:00,这意味着我们必须从最后一个字符串中减去一个以获得小时。

I tried datetime.strptime(date, '%Y %d %Y %I')我试过datetime.strptime(date, '%Y %d %Y %I')

datetime.strptime if to define the timestamp from a string, the format should match the provided one. datetime.strptime如果要从字符串定义时间戳,则格式应与提供的格式匹配。 datetime.strftime (note the f ) is to generate a string from a datetime object. datetime.strftime (注意f )是从 datetime 对象生成一个字符串。 You can use:您可以使用:

datetime.strptime(date, '%Y-%m-%d_%H').strftime('%Y/%m/%d %H:%M')

output: '2021/09/30 01:00'输出: '2021/09/30 01:00'

in case the _x defines a delta:如果_x定义了一个增量:

from datetime import datetime, timedelta
d, h = date.split('_')
d = datetime.strptime(d, '%Y-%m-%d')
h = timedelta(hours=int(h))
(d-h).strftime('%Y/%m/%d %H:%M')

output: '2021/09/29 23:00'输出: '2021/09/29 23:00'

Considering the _1 is hour and appears in al of your data (The hour part takes value between [1, 24]), your format was wrong.考虑到_1是小时并出现在您的所有数据中(小时部分取 [1, 24] 之间的值),您的格式是错误的。

For reading the date from string you'll need format it correctly:要从字符串中读取日期,您需要正确格式化它:

from datetime import datetime, timedelta

date = "2021-09-30_1"

date_part, hour_part = date.split("_")
date_object = datetime.strptime(date_part, '%Y-%m-%d') + timedelta(hours=int(hour_part) - 1)

Now you have the date object.现在你有了日期对象。 And you can display it as:您可以将其显示为:

print(date_object.strftime('%Y/%m/%d %H:%M'))
from datetime import datetime

raw_date = "2021-09-30_1"
date = raw_date.split("_")[0]
parsed_date = datetime.strptime(date, '%Y-%m-%d')
formated_date = parsed_date.strftime('%Y/%m/%d %H:%M')

strptime is used for parsing string and strftime for formating. strptime用于解析字符串, strftime用于格式化。

Also for date representation you should provide format codes for hours and minutes as in:同样对于日期表示,您应该提供小时和分钟的格式代码,如下所示:

formated_date = parsed_date.strftime('%Y/%m/%d %H:%M')

暂无
暂无

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

相关问题 ValueError:无法将字符串转换为浮点数:'2021-09-22' - ValueError: could not convert string to float: '2021-09-22' UTC 日期时间问题 - 如何适合冒号 - 时间数据“2021-03-11 09:30:01-05:00”与格式“%Y%m%d %H:%M:%S %z”不匹配' - Problems with UTC datetime - how to fit a colon - time data '2021-03-11 09:30:01-05:00' does not match format '%Y%m%d %H:%M:%S %z' 如何在 python 中将“2021-09-01T14:37:40.537Z”转换为“2021-09-01T14:37:40.5370000+00:00”? - how to convert "2021-09-01T14:37:40.537Z" into "2021-09-01T14:37:40.5370000+00:00" in python? 如何将日期时间格式化为 Python 中的“2021-04-30T10:30:00+12:00”? - How to format datetime to format "2021-04-30T10:30:00+12:00" in Python? 如何将“2019-11-30T07:00:00+09:00”日期转换为整数? - How to convert "2019-11-30T07:00:00+09:00" date into integer? 如何将“2020-09-30 23:45:27+0000”转换为日期对象? - How can I convert '2020-09-30 23:45:27+0000' to date object? 如何将字符串日期转换为日期对象并获取字符串日期值格式为 2021-01-22T11:36:52.387000+01:00 和日期时间的年份? - How to convert string date to dateobject and get the year when string date value is in format 2021-01-22T11:36:52.387000+01:00 with datetime? 如何使用python strptime和strftime将2017-02-09T09:43:04.216 + 1000等日期时间格式转换为2017年2月9日? - How to convert a datetime format like 2017-02-09T09:43:04.216+1000 to 09-Feb-2017 using python strptime and strftime? 如何将字符串转换为日期时间? - How to convert string into datetime? 如何将日期时间(“2020-09-27 07:14:01.114051200”)转换为周数? - How to convert Datetime ("2020-09-27 07:14:01.114051200") to week number?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM