租用问题

质量为本、客户为根、勇于拼搏、务实创新

< 返回租用问题列表

python中strptime的用法是什么,pythonstrftime

发布时间:2023-11-23 06:12:43

python中strptime的用法是甚么

strptime()是Python中用于将字符串转换为日期或时间对象的方法之一。它的完全语法以下:

datetime.datetime.strptime(date_string, format)

其中,date_string是要转换的字符串,format是日期或时间的格式。

示例:

import datetime

date_string = "2021-01-01"
date_object = datetime.datetime.strptime(date_string, "%Y-%m-%d")
print(date_object)

输出:

2021-01-01 00:00:00

在上面的示例中,date_string是一个表示日期的字符串,使用"%Y-%m-%d"作为格式,通过strptime()方法将其转换为datetime对象。