解析日期字符串并更改格式 [英] Parse date string and change format

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

问题描述

我有一个格式为Mon Feb 15 2010"的日期字符串.我想将格式更改为15/02/2010".我该怎么做?

解决方案

datetime 模块可以帮助您:

datetime.datetime.strptime(date_string, format1).strftime(format2)

对于你可以做的具体例子

<预><代码>>>>导入日期时间>>>datetime.datetime.strptime('Mon Feb 15 2010', '%a %b %d %Y').strftime('%d/%m/%Y')'15/02/2010'>>>

I have a date string with the format 'Mon Feb 15 2010'. I want to change the format to '15/02/2010'. How can I do this?

解决方案

datetime module could help you with that:

datetime.datetime.strptime(date_string, format1).strftime(format2)

For the specific example you could do

>>> import datetime
>>> datetime.datetime.strptime('Mon Feb 15 2010', '%a %b %d %Y').strftime('%d/%m/%Y')
'15/02/2010'
>>>

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

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