2017-01-04 17 views
0

私は現在の日付を取得し、それを単語フォーマットに変換するこのコードを持っています。日付文字列のフォーマットを変換する

import datetime 

days={'0':'Monday','1':'Tuesday','2':'Wednesday', 
     '3':'Thursday','4':'Friday','5':'Saturday', 
     '6':'Sunday'} 

months={'1':'January','2':'February','3':'March','4':'April', 
     '5':'May','6':'June','7':'July','8':'August', 
     '9':'September','10':'October','11':'November','12':'December'} 

currentDay=datetime.date.weekday(datetime.datetime.now()) 
currentMonth=datetime.date.today().month 
currentDate=str(datetime.date.today().day) 
if currentDate[:1]=="1":suffix="st" 
elif currentDate[:1]=="2":suffix="nd" 
elif currentDate[:1]=="2":suffix="rd" 
else:suffix="th" 
dateString=("{} the {}{} of {}".format(
    days[str(currentDay)],currentDate,suffix,months[str(currentMonth)])) 

今日のための出力、

>>>Wednesday the 4th of January 

どのように私はWord形式にフォーマットDD/MM/YYYYでユーザー入力を回す程度でしょうか?

+0

'適切な形式でdatetime.strptime'を使用しています。ドキュメントを参照してください。 –

答えて

0
あなたが行うことができます

from datetime import datetime 
user_input = "DD/MM/YYYY" 
datetime_object = datetime.strptime(user_input, "%d/%m/%Y") 
あなたのロジックが続く

currentDay=datetime_object.weekday() 
currentMonth=datetime_object.month 
currentDate=str(datetime_object.day) 
if currentDate[:1]=="1":suffix="st" 
elif currentDate[:1]=="2":suffix="nd" 
elif currentDate[:1]=="2":suffix="rd" 
else:suffix="th" 
dateString=("{} the {}{} of {}".format(
    days[str(currentDay)],currentDate,suffix,months[str(currentMonth)])) 
+0

ありがとうございますが、これはそのまま日付を返すので、今週の水曜日のように返信してください。 – badNameHere

+0

これ以降、週、あなたがすでにやっているように –

関連する問題