![]() |
Change format of datetime - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Change format of datetime (/thread-15872.html) |
Change format of datetime - Jonas85 - Feb-04-2019 Hi! I just got a e-paper display and downloaded a nice script to let it get weather, my schedule and such. The script does put the date for every schedule entry in the format YYYY-mm-dd, and I've found the code below that I believe puts this out. I would like to display this time and date in the format 04 feb instead, as now, 2019-02-04. I've spent hours and hours, and can't get it right. About to loose my mind here... tried every possible combination of datetime. due_date = int(datetime.date(int(str(my_task['due']['date']).split('-')[0]), int(str(my_task['due']['date']).split('-')[1]), int(str(my_task['due']['date']).split('-')[2])).strftime('%j')) + (int(str(my_task['due']['date']).split('-')[0]) * 365)I'd be incredibly happy if someone could give me a helping hand here! :) Best regards! RE: Change format of datetime - ichabod801 - Feb-04-2019 It sounds like you want the strftime method of the date object. >>> today = datetime.date.today() >>> today.strftime('%b %d') 'Feb 04'Full details on the format strings used are docs. RE: Change format of datetime - Jonas85 - Feb-04-2019 (Feb-04-2019, 06:53 PM)ichabod801 Wrote: It sounds like you want the strftime method of the date object. Thank's alot ichabod801, but no matter how I do it doesn't work. I'm quite new to python, but speak fluent PHP... but that doesn't seem to help right now hehe RE: Change format of datetime - ichabod801 - Feb-04-2019 What is not working? >>> text = '2019-04-02' >>> nums = [int(x) for x in text.split('-')] >>> dt = datetime.date(*nums) >>> dt.strptime('%b %d') 'Feb 04' RE: Change format of datetime - Jonas85 - Feb-05-2019 (Feb-04-2019, 08:52 PM)ichabod801 Wrote: What is not working? I've got this row above of code but can't use your script to get it right.. I tried this: nums = [int(x) for x in due_date.split('-')] >>> dt = datetime.date(*nums) >>> dt.strptime('%b %d')but with no luck. RE: Change format of datetime - ichabod801 - Feb-05-2019 If you want help here, you need to be more clear. I can't diagnose the problem with 'no luck'. What was due_date, what output did you get, and how was it not what you wanted? RE: Change format of datetime - Jonas85 - Feb-05-2019 I'm sorry ichabod801, I'm at my phone at the moment and should've waited til I could test it some more and give you more info that "no luck". I'll get back on this later :) Thanks again!, |