解析一个日期到datetime对象的字符串 [英] Parse a string with a date to a datetime object

查看:74
本文介绍了解析一个日期到datetime对象的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何解析一个字符串,如01-Jan-1995到Python datetime 对象?

How can I parse a string like "01-Jan-1995" to a Python datetime object?

推荐答案

总的来说,您将使用 time strptime 函数解析日期和时间字符串c $ c>或 datetime 模块。您的示例可以解析:

On the whole you'd parse date and time strings with the strptime functions in time or datetime modules. Your example could be parsed with:

import datetime
datetime.datetime.strptime("01-Jan-1995", "%d-%b-%Y")

请注意,解析月份名称与区域设置相关。

Note that parsing month names is locale-dependent.

这篇关于解析一个日期到datetime对象的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆