python - 将日期字符串转换为日期时间对象

标签 python

采用以下日期输入:

01/01/2012, 1-1-2012, 2012-01-20, 01012012, 20120101, 1.1.2012, 01.01.2012

我如何创建一个函数来将任何典型的日期字符串转换为 datetime.date 对象。我想你首先会删除所有非数字字符,然后进行一些正则表达式识别。

让我们假设,如果年在前,则月在第二,如果年在最后(第三),则月在前。这里最好的功能是什么?

最佳答案

使用strptime伴随着这个formatting table , split和一些 try/catch block 。

关于python - 将日期字符串转换为日期时间对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10657703/

相关文章:

python - 如何显示x和y轴图

python - 如何遍历 python 中的列表并获取每两个接近项之间的比较值的 bool 列表?

python - Python 中的 4 个子图条形图

python - 改变 Sprite 动画速度(pygame)

python - 我的 python Tornado 代码不起作用......为什么

python - 猫升压 : Cannot calc metric which requires logits for absolute values

python - 列表理解未按预期评估

python - (Discord.py) 固定消息

python - 按文本文件的行读取头、尾和向后

python - Keras图像预处理: Tuple Index Out of Range