I'm trying to calculate the difference between two dates in "weeks of year". I can get the datetime object and get the days etc but not week numbers. I can't, of course, subtract dates because weekends can't be ensured with that.
I tried getting the week number using d1.isocalendar()[1] and subtracting d2.isocalendar()[1] but the issue is that isocalendar()[1] returns December 31, 2012 as week 1 (which supposedly is correct) but that means my logic cannot span over this date.
For reference, here's my complete code:
def week_no(self):
ents = self.course.courselogentry_set.all().order_by('lecture_date')
l_no = 1
for e in ents:
if l_no == 1:
starting_week_of_year = e.lecture_date.isocalendar()[1] # get week of year
initial_year = e.lecture_date.year
if e == self:
this_year = e.lecture_date.year
offset_week = (this_year - initial_year) * 52
w_no = e.lecture_date.isocalendar()[1] - starting_week_of_year + 1 + offset_week
break
l_no += 1
return w_no
With this code, the lecture on Dec 31, 2012 ends up being -35.
How about calculating the difference in weeks between the Mondays within weeks of respective dates? In the following code, monday1 is the Monday on or before d1 (the same week):
from datetime import datetime, timedelta
monday1 = (d1 - timedelta(days=d1.weekday()))
monday2 = (d2 - timedelta(days=d2.weekday()))
print 'Weeks:', (monday2 - monday1).days / 7
Returns 0 if both dates fall withing one week, 1 if on two consecutive weeks, etc.
You may want to refer the Python CookBook (2005 edition) Recipe 3.3. The following code snippet is from the cookbook, does what you require.
from dateutil import rrule
import datetime
def weeks_between(start_date, end_date):
weeks = rrule.rrule(rrule.WEEKLY, dtstart=start_date, until=end_date)
return weeks.count()
This is a very simple solution with less coding everyone would understand.
from datetime import date
d1 = date(year, month, day)
d2 = date(year, month, day)
result = (d1-d2).days//7
The solution above has a bug (I can't add a comment due to low reputation). It doesn't account for hour differences.
# This code has a bug.
monday1 = (d1 - timedelta(days=d1.weekday()))
monday2 = (d2 - timedelta(days=d2.weekday()))
Counter example of 2 dates more than a week apart:
Timestamp1: 1490208193270795 (22 Mar 2017 18:43:13 GMT)
Monday1: 20 Mar 2017 18:43:13 GMT
Timestamp2: 1489528488744290 (14 Mar 2017 21:54:48 GMT)
Monday2: 13 Mar 2017 21:54:48 GMT
Using that code it returns 0 as week diff when it should be 1. Need to zero out the hours/minutes/seconds as well.
To determine how many weeks are spanned by two dates. eg From 3rd Oct to 13th Oct
October 2015
Mo 5 12 19 26
Tu 6 13 20 27
We 7 14 21 28
Th 1 8 15 22 29
Fr 2 9 16 23 30
Sa 3 10 17 24 31
Su 4 11 18 25
Code:
import math, datetime
start_date = datetime.date(2015, 10, 3)
start_date_monday = (start_date - datetime.timedelta(days=start_date.weekday()))
end_date = datetime.date(2015, 10, 13)
num_of_weeks = math.ceil((end_date - start_date_monday).days / 7.0)
Equals 3 weeks.
You're a bit vague on what 'difference in weeks' means exactly. Is 6 days difference one week or zero ? Is eight days difference one week or two ?
In any case, why can't you simply find the difference in another unit (seconds or days) and divide by the appropriate amount, with your prefered rounding for weeks?
Edited Best Answer
from datetime import timedelta
monday1 = (d1 - timedelta(days=d1.weekday()))
monday2 = (d2 - timedelta(days=d2.weekday()))
diff = monday2 - monday1
noWeeks = (diff.days / 7) + math.ceil(diff.seconds/86400)
print('Weeks:', noWeeks)`
Related
Python pandas (0.24.1) is adding a seemingly arbitrary number of hours, minutes, and seconds to my datetime objects. This seems unexpected as default behavior; I would expect the time component to default to midnight (00:00:00). Is this a bug?
import pandas as pd
df = pd.DataFrame( {'yr': [2019, 2019],
'mo': [9, 9],
'dy': [25, 26]} )
df['dtime'] = ( pd.to_datetime(df['yr'],format='%Y')
+pd.to_timedelta(df['mo']-1,unit='M')
+pd.to_timedelta(df['dy']-1,unit='d') )
print('pandas version == '+pd.__version__)
df
################################################
OUTPUT:
################################################
pandas version == 0.24.1
yr mo dy dtime
0 2019 9 25 2019-09-25 11:52:48
1 2019 9 26 2019-09-26 11:52:48
Problem is with converting months, here is used 'rounded' year (because leap year) and divided by 12 for 'rounded' month:
print (pd.to_timedelta(365.2425, unit='d') / 12)
30 days 10:29:06
print (pd.to_timedelta(1, unit='M'))
30 days 10:29:06
print (pd.to_timedelta(df['mo']-1,unit='M'))
0 243 days 11:52:48
1 243 days 11:52:48
Name: mo, dtype: timedelta64[ns]
Better solution is use to_datetime with year, monht and day columns and if necessary filter it by subset with list(d.values()) (if another columns in real data):
d = {'yr':'year', 'mo':'month', 'dy':'day'}
df['dtime'] = pd.to_datetime(df.rename(columns=d)[list(d.values())])
print (df)
yr mo dy dtime
0 2019 9 25 2019-09-25
1 2019 9 26 2019-09-26
To add detail as to the issue with timedelta that Jezrael pointed out above, the issue with the month conversion is as follows: Pandas timedelta defines a month as 1/12 of a year, which is 365.2425 days based on leap year logic.
243 days 11:52:48 is 21037968 seconds.
>>> 243*60*60*24+11*60*60+52*60+48
21037968
Some dimensional analysis confirms this is 8/12 of a year that is 365.2425 days long.
>>> 21037968/((8/12)*365.2425*60*60*24)
1.0
As noted above, use to_datetime to avoid this.
I want to create a function that will return a date depending on each month. More specific I want it to return to me the 23d day of each month in the example format (YYYY, MM, DD):
2018, 1, 23 .
except for the occasion that this date coincides with a weekend, in which I want it to return the exact previous working day. For example for December 2017 the 23d was Saturday, so in this case I want my function to return to me
2017, 12, 22.
Right now I use:
import datetime
someday = datetime.date(2018, 1, 23)
print(someday)
with which in the someday variable I define the desired date manually for each month.
In short for the next seven months of 2018 (January, February, March, April, May, June, July), I want my function to return to me :
2018, 1, 23
2018, 2, 23
2018, 3, 23
2018, 4, 23
2018, 5, 23
2018, 6, 22
2018, 7, 23
and so on and so forth for the next months.
You can achieve this with weekday(), timedelta() and while loop:
from datetime import date, timedelta
def last_workday(somedate):
while somedate.weekday() > 4: #weekday returns values 0-6 (Mon-Sun)
somedate -= timedelta(days=1) #timedelta returns 1 day which you subtract from your date
return somedate
y = 2018
d = 23
for m in range(1,13):
print(last_workday(date(y, m, d)))
I wrote a small python script on how you could do it.
Use toordinal to check for the day of the week and if it's Saturday or Sunday, subtract 1 or 2 by using timedelta
import datetime
# Yes, from 1,13 because 13 is exclusive (Goes only to 12)
days_month = datetime.monthrange(2018,x)
for x in range(days_month[0],days_month[1]+1):
for x in range(1, monthrange):
someday = datetime.date(2018, x, 23) # Loop over every month
weekday = someday.weekday() # 0 = Monday, 6 = Sunday
if(weekday > 5 ): # If it's "more" than Friday
jumpback_days = weekday - 4; # Subtract 4 from the current weekday to get 1 for Saturday and 2 for Sunday
print(someday - datetime.timedelta(days=jumpback_days)) # Subtract days and print
else:
# Print without subtracting anything
print(someday)
Note: If you are using Python2, please replace range with xrange for it to work.
EDIT: If you want to print only and all workdays in a specific year, you can do it this way:
import datetime
from calendar import monthrange
year = 2018
for month in range(1, 13): # Yes, from 1,13 because 13 is exclusive (Goes only to 12)
days_month = monthrange(year, month) # Request the amount of days in that month
for day in range(1, days_month[1] + 1): # Iterate through all days of that month (+1 because it's exclusive)
someday = datetime.date(year, month, day) # Loop over every month
weekday = someday.weekday() # 0 = Monday, 6 = Sunday
if(weekday < 5 ): # If it's "less" than Saturday
# Print because if it's "less" than Saturday, then it's a workday
print(someday)
I'm trying to write a program that shows the days in a month when you type the number that corresponds to the month.
Ex. 1 = January, would print "31"
This is what I have and it seems logical to me. Although I'm just over a month into this and I have no idea what I'm doing.
def calcDaysInMonth():
(list,(range,(1, 12)))
a = raw_input()
int(a)
jan = 1
feb = 2
mar = 3
apr = 4
may = 5
june = 6
july = 7
aug = 8
sept = 9
octo = 10
nov = 11
dec = 12
if jan is True:
print("31")
using static number wont help you get correct result. because Feb days in leap year is different than normal. so use
$Year = 2017
$month = 08`
echo cal_days_in_month(CAL_GREGORIAN, (int)$month, $Year);
The reason your code isn't working is because you're assigning the input to a but you're never checking the value of a and using that to determine what should be printed (you're simply assigning integers to variables called jan, feb etc)
You're looking for something like this:
a = int(raw_input())
if a == 1:
print("31 days")
elif a == 2:
print("28 days")
# just repeat with elif until december/12
You could try to get clever with it with dictionaries to map months to days or something, but really a more sensible solution would be the following...
Due to February having a different number of days given leap years, it makes more sense to just use calendar.monthrange to get the number of days in a month for any given year:
from calendar import monthrange
year = 2017
a = int(raw_input())
num_days = monthrange(year, a)[1]
print("{} days".format(num_days))
Thank you for all your help guys.
My class now has an answer for what we were doing. What was wanted:
month = int(raw_input())
day = 0
def calcDays(month):
if month ==1:
days = 31
print 31
if month==2:
days = 28
print 28
if month == 3:
days = 31
print 31
if month == 4:
days = 30
print 30
if month==5:
days = 31
print 31
if month ==6:
days = 30
print 30
if month==7:
days = 31
print 31
if month ==8:
days = 31
print 31
if month==9:
days = 30
print 30
This question already has answers here:
How do I calculate the date six months from the current date using the datetime Python module?
(47 answers)
Closed 7 years ago.
So I am trying to find a way to increment a datetime object by one month. However, it seems this is not so simple, according to this question.
I was hoping for something like:
import datetime as dt
now = dt.datetime.now()
later = now + dt.timedelta(months=1)
But that doesn't work. I was also hoping to be able to go to the same day (or the closest alternative) in the next month if possible. For example, a datetime object set at January 1st would increment to Feb 1st whereas a datetime object set at February 28th would increment to March 31st as opposed to March 28th or something.
To be clear, February 28th would (typically) map to March 31st because it is the last day of the month, and thus it should go to the last day of the month for the next month. Otherwise it would be a direct link: the increment should go to the day in the next month with the same numbered day.
Is there a simple way to do this in the current release of Python?
Check out from dateutil.relativedelta import *
for adding a specific amount of time to a date, you can continue to use timedelta for the simple stuff i.e.
import datetime
from dateutil.relativedelta import *
use_date = datetime.datetime.now()
use_date = use_date + datetime.timedelta(minutes=+10)
use_date = use_date + datetime.timedelta(hours=+1)
use_date = use_date + datetime.timedelta(days=+1)
use_date = use_date + datetime.timedelta(weeks=+1)
or you can start using relativedelta
use_date = use_date+relativedelta(months=+1)
use_date = use_date+relativedelta(years=+1)
for the last day of next month:
use_date = use_date+relativedelta(months=+1)
use_date = use_date+relativedelta(day=31)
Right now this will provide 29/02/2016
for the penultimate day of next month:
use_date = use_date+relativedelta(months=+1)
use_date = use_date+relativedelta(day=31)
use_date = use_date+relativedelta(days=-1)
last Friday of the next month:
use_date = use_date+relativedelta(months=+1, day=31, weekday=FR(-1))
2nd Tuesday of next month:
new_date = use_date+relativedelta(months=+1, day=1, weekday=TU(2))
As #mrroot5 points out dateutil's rrule functions can be applied, giving you an extra bang for your buck, if you require date occurences.
for example:
Calculating the last day of the month for 9 months from the last day of last month.
Then, calculate the 2nd Tuesday for each of those months.
from dateutil.relativedelta import *
from dateutil.rrule import *
from datetime import datetime
use_date = datetime(2020,11,21)
#Calculate the last day of last month
use_date = use_date+relativedelta(months=-1)
use_date = use_date+relativedelta(day=31)
#Generate a list of the last day for 9 months from the calculated date
x = list(rrule(freq=MONTHLY, count=9, dtstart=use_date, bymonthday=(-1,)))
print("Last day")
for ld in x:
print(ld)
#Generate a list of the 2nd Tuesday in each of the next 9 months from the calculated date
print("\n2nd Tuesday")
x = list(rrule(freq=MONTHLY, count=9, dtstart=use_date, byweekday=TU(2)))
for tuesday in x:
print(tuesday)
Last day
2020-10-31 00:00:00
2020-11-30 00:00:00
2020-12-31 00:00:00
2021-01-31 00:00:00
2021-02-28 00:00:00
2021-03-31 00:00:00
2021-04-30 00:00:00
2021-05-31 00:00:00
2021-06-30 00:00:00
2nd Tuesday
2020-11-10 00:00:00
2020-12-08 00:00:00
2021-01-12 00:00:00
2021-02-09 00:00:00
2021-03-09 00:00:00
2021-04-13 00:00:00
2021-05-11 00:00:00
2021-06-08 00:00:00
2021-07-13 00:00:00
rrule could be used to find the next date occurring on a particular day.
e.g. the next 1st of January occurring on a Monday (Given today is the 4th November 2021)
from dateutil.relativedelta import *
from dateutil.rrule import *
from datetime import *
year = rrule(YEARLY,dtstart=datetime.now(),bymonth=1,bymonthday=1,byweekday=MO)[0].year
year
2024
or the next 5 x 1st of January's occurring on a Monday
years = rrule(YEARLY,dtstart=datetime.now(),bymonth=1,bymonthday=1,byweekday=MO)[0:5]
for i in years:print(i.year)
...
2024
2029
2035
2046
2052
The first Month next Year that starts on a Monday:
>>> month = rrule(YEARLY,dtstart=datetime.date(2023, 1, 1),bymonthday=1,byweekday=MO)[0]
>>> month.strftime('%Y-%m-%d : %B')
'2023-05-01 : May'
If you need the months that start on a Monday between 2 dates:
months = rrule(YEARLY,dtstart=datetime.date(2025, 1, 1),until=datetime.date(2030, 1, 1),bymonthday=1,byweekday=MO)
>>> for m in months:
... print(m.strftime('%Y-%m-%d : %B'))
...
2025-09-01 : September
2025-12-01 : December
2026-06-01 : June
2027-02-01 : February
2027-03-01 : March
2027-11-01 : November
2028-05-01 : May
2029-01-01 : January
2029-10-01 : October
This is by no means an exhaustive list of what is available.
Documentation is available here: https://dateutil.readthedocs.org/en/latest/
Note: This answer shows how to achieve this using only the datetime and calendar standard library (stdlib) modules - which is what was explicitly asked for. The accepted answer shows how to better achieve this with one of the many dedicated non-stdlib libraries. If you can use non-stdlib libraries, by all means do so for these kinds of date/time manipulations!
How about this?
def add_one_month(orig_date):
# advance year and month by one month
new_year = orig_date.year
new_month = orig_date.month + 1
# note: in datetime.date, months go from 1 to 12
if new_month > 12:
new_year += 1
new_month -= 12
new_day = orig_date.day
# while day is out of range for month, reduce by one
while True:
try:
new_date = datetime.date(new_year, new_month, new_day)
except ValueError as e:
new_day -= 1
else:
break
return new_date
EDIT:
Improved version which:
keeps the time information if given a datetime.datetime object
doesn't use try/catch, instead using calendar.monthrange from the calendar module in the stdlib:
import datetime
import calendar
def add_one_month(orig_date):
# advance year and month by one month
new_year = orig_date.year
new_month = orig_date.month + 1
# note: in datetime.date, months go from 1 to 12
if new_month > 12:
new_year += 1
new_month -= 12
last_day_of_month = calendar.monthrange(new_year, new_month)[1]
new_day = min(orig_date.day, last_day_of_month)
return orig_date.replace(year=new_year, month=new_month, day=new_day)
Question: Is there a simple way to do this in the current release of Python?
Answer: There is no simple (direct) way to do this in the current release of Python.
Reference: Please refer to docs.python.org/2/library/datetime.html, section 8.1.2. timedelta Objects. As we may understand from that, we cannot increment month directly since it is not a uniform time unit.
Plus: If you want first day -> first day and last day -> last day mapping you should handle that separately for different months.
>>> now
datetime.datetime(2016, 1, 28, 18, 26, 12, 980861)
>>> later = now.replace(month=now.month+1)
>>> later
datetime.datetime(2016, 2, 28, 18, 26, 12, 980861)
EDIT: Fails on
y = datetime.date(2016, 1, 31); y.replace(month=2) results in ValueError: day is out of range for month
Ther is no simple way to do it, but you can use your own function like answered below.
Can someone tell me how to get a week number in Python for actual calendar.
Ex: 2016-01-01 to 2016-01-07 = week 1
2016-01-08 to 2016-01-14 = week 2
I tried 2 ways but none of them are working
Using isocalendar()
But that does not work for year end and year start week. For ex:
datetime.date(2016,01,01).isocalendar()[1]
# should be: 1
# got 53
is the following:
dt = date(2016,01,02)
d = strftime("%U", time.strptime(str(dt),"%Y-%m-%d"))
print d
# 00
dt = date(2016,01,03)
d = strftime("%U", time.strptime(str(dt),"%Y-%m-%d"))
print d
# 01
Both the ways do not satisfy my requirement. Is there any other library I can use to get the week number in Actual Calendar ?
Are you talking about actual number of 7 day periods rather than which week number you're in? Keep in mind in 2016, Jan 3rd is the first day of the second week.
If you're looking at which 7 day period, you should simply count days since the beginning of the year and floor div by 7
dt = datetime.datetime(year=2016, month=1, day=2)
days = dt - datetime.datetime(year=dt.year, month=1, day=1).days
weeks = days // 7 + 1
The 29th should be the first day of week 5. Let's try it.
dt = datetime.datetime(year=2016, month=1, day=29)
days = dt - datetime.datetime(year=dt.year, month=1, day=1).days
weeks = days // 7 + 1
# 5