2017-09-15 62 views
-1

我有一个日期为2015年5月05日。我希望在2015年5月5日。在另一行我也希望它在05052015.How做到这一点使用PythonPython中日期格式的转换

+0

请试着问一个问题之前,这里寻找答案自己。 – user3151902

+0

欢迎来到Stack Overflow!请回顾[**如何问**](https://stackoverflow.com/help/how-to-ask)关于Stack Overflow的问题和[可以问什么类型的问题](https://stackoverflow.com/help/on-topic)和[应该避免哪些类型](https://stackoverflow.com/help/dont-ask) – bhansa

+0

https://docs.python.org/2/library/datetime.html – Will

回答

2

我建议你读Python datetime library

In [28]: from datetime import datetime 

In [29]: my_date = '05 May, 2015' 

In [30]: custom_date = datetime.strptime(my_date,'%d %B, %Y') 

In [34]: first_date_format = custom_date.strftime('%d-%B-%Y') 

In [35]: first_date_format 
Out[35]: '05-May-2015' 

In [36]: second_date_format = custom_date.strftime('%d%m%Y') 

In [37]: second_date_format 
Out[37]: '05052015' 
+0

I有一个'05年5月,1800'的日期。如何将上述格式转换成 – Esha

+0

@Esha同样适用。 –