Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Change format of datetime
#1
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!
Quote
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#3
(Feb-04-2019, 06:53 PM)ichabod801 Wrote: 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.

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
Quote
#4
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'
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#5
(Feb-04-2019, 08:52 PM)ichabod801 Wrote: 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'

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.
Quote
#6
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?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#7
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!,
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Definition of .format? (Newbie question) Jan_97 4 179 Jul-13-2019, 12:49 PM
Last Post: Jan_97
  changing format to Int Scott 3 243 Jun-19-2019, 06:33 AM
Last Post: ODIS
  Date format issue the_dude 11 631 Jun-07-2019, 06:27 AM
Last Post: snippsat
  Changing Number Format moby 4 196 May-24-2019, 11:04 PM
Last Post: snippsat
  changing { and } in str.format() Skaperen 10 459 May-16-2019, 05:07 AM
Last Post: Skaperen
  Return in correct json format UtiliseIT 3 296 May-13-2019, 11:24 AM
Last Post: snippsat
  error: self.name = "NPC{}".format(num_of_NPCs + 1) Exsul 1 118 May-03-2019, 08:56 PM
Last Post: ichabod801
  Format for elif sunilramakumar 4 307 Apr-08-2019, 05:21 PM
Last Post: Yoriz
  Convert SAS Dates Format in a loop MohanReddy 2 245 Apr-02-2019, 10:31 AM
Last Post: scidam
  Help regarding date format Prince_Bhatia 2 212 Apr-01-2019, 05:46 AM
Last Post: Prince_Bhatia

Forum Jump:


Users browsing this thread: 2 Guest(s)