One - One Code All

Blog Content

Python 中将字符串转换为 DateTime 对象

Python   2017-06-07 12:27:21

最常见的格式代码,你可以随时将字符串转换为日期。


以下是一些最常见的:


%Y - 用于表示年份,范围从 0001 到 9999

%m - 用于表示一年中的月份,范围从 01 到 12

%d - 用于表示月份中的天数,范围从 01 到 31

%H — 用于以 24 小时格式表示一天中的小时,范围从 00 到 23

%I — 用于以 12 小时格式表示一天中的小时,范围从 01 到 12

%M — 用于表示一小时中的分钟,范围从 00 到 59

%S — 用于表示一分钟内的秒数,范围也从 00 到 59

from datetime import datetime

date_string = "2018/08/09"

format = %Y/%m/%d #specifify the format of the date_string.

date = datetime.strptime(date_string, format)
print(date)



上一篇:python如何将字符串作为变量名,locals,vars,eval
下一篇:python重新加载模块reload

The minute you think of giving up, think of the reason why you held on so long.