I want to add "%m-%d-%H-%M" to "%m-%d-%H-%M" without additional python libraries. I am using now datetime and timedelta, but timedelta don't understand months and I also have problem with rounding date (for example "%06-%80-%99-%99" + "%07-%17-%20-%12").
Asked
Active
Viewed 115 times
1
-
I found this [link](https://stackoverflow.com/questions/546321/how-do-i-calculate-the-date-six-months-from-the-current-date-using-the-datetime) question but I don't know does this method round days to months and months to years. And it also uses extension relativedelta that I don't want to install on my host, because it will be difficult for me – Ислам Малахов Aug 19 '21 at 23:50
-
`datetime.relativedelta` knows how to do all of that and more. And it's open-source: if installing the library is such a problem, just incorporate it in your own project. – BoarGules Aug 20 '21 at 07:22
1 Answers
0
Not sure what result do you want here?
What is a month in this case? February can be of various size dependent on year, for example. If you add "three month" to June, this is one case, if "to July" - another, since different months has different day numbers.
Looks like you can have "100 minutes 100 hours 100 days 100 month"? Then just add months to months, days to days?
Anyhow, adding large time ranges like month and years, I'd probably went via seconds, relative to the year 1 AD (in Python there is no 0 year). Where questions like "what does it mean to add one month and one year" will still be relevant. So one might want to "add another year in seconds", etc.
from datetime import datetime, timedelta
time1 = datetime.strptime('28 01 2021', '%d %m %Y')
time_min = datetime.min
time2 = datetime.strptime('15 01 2021', '%d %m %Y')
time = (time1 - time_min).total_seconds() + (time2 - time_min).total_seconds()
#year_of_seconds = (datetime(2,1,1) - time_min).total_seconds()
(time_min + timedelta(seconds = time)).strftime('%d %m %Y')
11 02 4041

Artyrm Sergeev
- 189
- 7
-
If I just add months to months and days to days I won't know how to round extra days considering the different lengths of months, I thought smth simple like timedelta exists for months. In this case I think I can ignore leap year and just cut extra months, so I have problem only with days – Ислам Малахов Aug 20 '21 at 01:44
-
and it also is not comfortable to manually round every digit in datetime – Ислам Малахов Aug 20 '21 at 01:51
-
Still not sure, what result do you want, and what is "round every digit in datetime". But if you're fine adding days to days and months to months, just treat them as simple numbers: ` import numpy as np first_date = [6, 80, 99, 99] second_date = [7, 17, 20, 12] np.add(first_date, second_date) ` >array([ 13, 97, 119, 111]) – Artyrm Sergeev Aug 20 '21 at 02:02
-
At first I didn't notice your code (I am new to this site), thank you for this simple idea of converting date to seconds and adding seconds! – Ислам Малахов Aug 20 '21 at 02:16
-
I'm glad if it would help :) note that adding "1d 1m" if it is a _date_ you basically add only 0 days, since at 1 month of the year _zero_ months passed yet. Also, feel free to upvote (those up triangles near the answers and comments). – Artyrm Sergeev Aug 20 '21 at 02:18
-
1I need a reputation on this site for an upvote, but I mentioned your answer like the best – Ислам Малахов Aug 20 '21 at 14:18