I need to calculate the date based on the number of minutes from the start of the year. I just need the day and month.
I have tried a few different classes within Python datetime but I can not get the desired result.
for example, at 1700 today, minutes since the 01/01/2022 is 263100
print(datetime.timedelta(0,0,0,0,263100))
This returns
182 days, 17:00:00
Before I explicitly write out the months and how many days they have and work it out that way, is there something already built in that I am missing within datetime?
CodePudding user response:
I think this is what you are looking for. We add the minutes timedelta
to the start datetime
, and return it formatted.
from datetime import datetime, timedelta
def datesince_min(min:int, start:tuple=(2022,1,1)) -> str:
date = datetime(*start) timedelta(minutes=min)
return date.strftime("%A, %B %d, %Y %I:%M:%S")
print(datesince_min(263100)) #Saturday, July 02, 2022 06:00:00
For more information regarding strftime
formatting, refer to this.
Before I explicitly write out the months and how many days they have and work it out that way...
You shouldn't ever have to do that, and if you did it would almost certainly have to be for a system or language that is in development.
CodePudding user response:
I found this code that should do the trick:
number_of_days = ((datetime.timedelta(0,0,0,0,263100)).split())[0]
months = (number_of_days - years * 365) // 30
days = (number_of_days - years * 365 - months*30)
print(months, days)
Where you replace 263100 with whatever minutes you wish ofc
:)