[英]Python convert date format
給定這樣的字符串:
2020-08-14
如何將其轉換為:
14 August 2020
使用 python 3?
您可以使用datetime
模塊重新格式化日期字符串。 使用strptime
您可以從字符串讀取到datetime
時間 object,然后使用strftime
您可以轉換回具有不同格式的字符串。
>>> from datetime import datetime
>>> d = datetime.strptime('2020-08-14', '%Y-%m-%d')
>>> d
datetime.datetime(2020, 8, 14, 0, 0)
>>> d.strftime('%d %B %Y')
'14 August 2020'
使用下面的 pandas function 的替代方法:
import pandas as pd
d = pd.to_datetime('2020-08-14')
d.strftime('%d %B %Y')
Out[11]: '14 August 2020'
monthDict = {
'01' : 'January',
'02' : 'February',
'03' : 'March',
'04' : 'April',
'05' : 'May',
'06' : 'June',
'07' : 'July',
'08' : 'August',
'09' : 'September',
'10' : 'October',
'11' : 'November',
'12' : 'December'
}
readDate = input('Pleaes enter the date in the yyyy-mm-dd format: ')
day = readDate.split('-')[2]
month = readDate.split('-')[1]
year = readDate.split('-')[0]
print('Printing in the dd month year format: ', day, monthDict[month], year)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.