在python中将字符串日期转换为日期格式? [英] Convert string date into date format in python?

查看:673
本文介绍了在python中将字符串日期转换为日期格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将以下字符串日期转换为python中的日期格式。

How to convert the below string date into date format in python.

input:
date='15-MARCH-2015'

expected output:
2015-03-15

我试图使用 datetime.strftime datetime.strptime 。它不接受这种格式。

I tried to use datetime.strftime and datetime.strptime. it is not accepting this format.

推荐答案

可以使用 datetime.strptime 使用正确的格式:

You can use datetime.strptime with a proper format :

>>> datetime.strptime('15-MARCH-2015','%d-%B-%Y')
datetime.datetime(2015, 3, 15, 0, 0)

详细了解 datetime.strptime 和日期格式:https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior

Read more about datetime.strptime and date formatting: https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior

这篇关于在python中将字符串日期转换为日期格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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