Modify days counter on datetime [Python] - python

I have this code and I want to count the days between 2 dates.
from datetime import date, datetime
checkin= datetime(2022, 1, 30, 1, 15, 00)
checkout= datetime(2022, 1, 31, 0, 0, 00)
count_days= (checkout - checkin).days
In this case, the result of count_days result is 0, because in an operation with 2 datetimes, it takes into account hours, minutes and seconds.
I want the result to be 1 because is +1 day of difference. Type of variables must be datetimes. Thanks!

Convert them to dates first, with the date method.
from datetime import date, datetime
checkin = datetime(2022, 1, 30, 1, 15, 00)
checkout = datetime(2022, 1, 31, 0, 0, 00)
count_days = (checkout.date() - checkin.date()).days

Could you do something like this?
(assuming you want a minimum since the solution you have is similar)
from datetime import date, datetime
check_in= datetime(2022, 1, 30, 1, 15, 00)
check_out= datetime(2022, 1, 31, 0, 0, 00)
# Number of days between two dates (min 1 day)
days = (check_out - check_in).days + 1
print(days)

Related

How to find Date Time Range in Python

time range like this
strart_date_time = datetime(2021, 1, 1, 5, 00, 00)
end_date_time = datetime(2021,1, 3, 00,00,00)
I want Output Like This I mean I want every single second count between given time range hope you understand.....
ThankYou in advance
output-
2021, 1, 1, 0, 00, 01
2021, 1, 1, 0, 01, 02
2021, 1, 1, 2, 30, 03
...
You can get the time difference in seconds, loop and display new date at each second
from datetime import datetime, timedelta
start_date_time = datetime(2021, 1, 1, 5, 00, 00)
end_date_time = datetime(2021,1, 3, 00,00,00)
time_diff = int((end_date_time - start_date_time).total_seconds())
for i in range(time_diff):
print(start_date_time + timedelta(0, i))

i want to get all dates exclude weekends between two dates in python

I want to get all the dates between two dates excluding weekends dates.
Below is the format of dates:
last_date = '2019-01-21'
curr_date = '2019-02-04'
Using date.weekday()
Return the day of the week as an integer, where Monday is 0 and Sunday
is 6.
from datetime import timedelta, date
def daterange(date1, date2):
for n in range(int ((date2 - date1).days)+1):
yield date1 + timedelta(n)
start_dt = date(2019,1,21)
end_dt = date(2019,2,4)
weekdays = [5,6]
for dt in daterange(start_dt, end_dt):
if dt.weekday() not in weekdays: # to print only the weekdates
print(dt.strftime("%Y-%m-%d"))
EDIT:
Using date.isoweekday():
Return the day of the week as an integer, where Monday is 1 and Sunday
is 7.
weekdays = [6,7]
for dt in daterange(start_dt, end_dt):
if dt.isoweekday() not in weekdays:
print(dt.strftime("%Y-%m-%d"))
OUTPUT:
2019-01-21
2019-01-22
2019-01-23
2019-01-24
2019-01-25
2019-01-28
2019-01-29
2019-01-30
2019-01-31
2019-02-01
2019-02-04
You can do this quite nicely with the dateutil library's rrule.
Example:
from dateutil.parser import parse
from dateutil.rrule import rrule, DAILY, MO, TU, WE, TH, FR
last_date = '2019-01-21'
curr_date = '2019-02-04'
result = rrule(
DAILY,
byweekday=(MO,TU,WE,TH,FR),
dtstart=parse(last_date),
until=parse(curr_date)
)
print(list(result))
Result:
[datetime.datetime(2019, 1, 21, 0, 0),
datetime.datetime(2019, 1, 22, 0, 0),
datetime.datetime(2019, 1, 23, 0, 0),
datetime.datetime(2019, 1, 24, 0, 0),
datetime.datetime(2019, 1, 25, 0, 0),
datetime.datetime(2019, 1, 28, 0, 0),
datetime.datetime(2019, 1, 29, 0, 0),
datetime.datetime(2019, 1, 30, 0, 0),
datetime.datetime(2019, 1, 31, 0, 0),
datetime.datetime(2019, 2, 1, 0, 0),
datetime.datetime(2019, 2, 4, 0, 0)]

Datetime object from a string for 15 days frequency

I am trying to code a function called days15(). The function will be passed an argument called ‘myDateStr’. myDateStr is string representation of a date in the form 20170817 (that is YearMonthDay). The code in the function will create a datetime object from the string, it will then create a timedelta object with a length of 1 day. Then, it will use a list comprehension to produce a list of 15 datetime objects, starting with the date that is passed to the function
the function should return the following list.
[datetime.datetime(2017, 8, 17, 0, 0), datetime.datetime(2017, 8, 18, 0, 0), datetime.datetime(2017, 8, 19, 0, 0), datetime.datetime(2017, 8, 20, 0, 0), datetime.datetime(2017, 8, 21, 0, 0), datetime.datetime(2017, 8, 22, 0, 0), datetime.datetime(2017, 8, 23, 0, 0), datetime.datetime(2017, 8, 24, 0, 0), datetime.datetime(2017, 8, 25, 0, 0), datetime.datetime(2017, 8, 26, 0, 0), datetime.datetime(2017, 8, 27, 0, 0), datetime.datetime(2017, 8, 28, 0, 0), datetime.datetime(2017, 8, 29, 0, 0), datetime.datetime(2017, 8, 30, 0, 0), datetime.datetime(2017, 8, 31, 0, 0)]
I am stuck for the code. I have strted with the below.Please help. Thanks
from datetime import datetime, timedelta
myDateStr = '20170817'
def days15(myDateStr):
Pandas will help you in converting strings to datetime, so first you need to import it:
from datetime import datetime, timedelta
import pandas as pd
myDateStr = '20170817'
Then you can initialize an empty list that you'll later append:
datelist = []
And then you write a function:
def days15(myDateStr):
#converting to datetime
date = pd.to_datetime(myDateStr)
#loop to create 15 datetimes
for i in range(15):
newdate = date + timedelta(days=i)
#adding new dates to the list
datelist.append(newdate)
and then you can call your function and get a list of 15 datetimes:
days15(myDateStr)
As you said, there will be two steps to implement: firstly, convert the string date to a datetime object and secondly, iterate over the next 15 days using timedelta, with a list comprehension or a simple loop.
from datetime import datetime, timedelta
myDateStr = '20170817'
# Parse the string and return a datetime object
def getDateTime(date):
return datetime(int(date[:4]),int(date[4:6]),int(date[6:]))
# Iterate over the timedelta added to the starting date
def days15(myDateStr):
return [getDateTime(myDateStr) + timedelta(days=x) for x in range(15)]

rrule weekly for three weeks, then one week not, repeat

I'm using rrule from python dateutil and don't know how to create an rruleset for the following example:
Monday, three weeks in a row. Then a week not, then again three weeks in a row, one not, and so on.
Any advice on creating an rrule(set) for this?
One way to do this is to use an rruleset with a WEEKLY rrule and a corresponding exrule for every 4th week:
from dateutil.rrule import rrule, rruleset
from dateutil.rrule import WEEKLY
from dateutil.relativedelta import relativedelta
from datetime import datetime, timedelta
dtstart = datetime(2011, 1, 1)
rrset = rruleset()
weekly_rule = rrule(freq=WEEKLY, dtstart=dtstart)
every_4_weeks = rrule(freq=WEEKLY, interval=4,
dtstart=dtstart + relativedelta(weeks=4))
rrset.rrule(weekly_rule)
rrset.exrule(every_4_weeks)
rrset.between(dtstart, dtstart + timedelta(days=65))
The result:
[datetime.datetime(2011, 1, 8, 0, 0),
datetime.datetime(2011, 1, 15, 0, 0),
datetime.datetime(2011, 1, 22, 0, 0),
datetime.datetime(2011, 2, 5, 0, 0),
datetime.datetime(2011, 2, 12, 0, 0),
datetime.datetime(2011, 2, 19, 0, 0),
datetime.datetime(2011, 3, 5, 0, 0)]
The way it works is weekly_rule generates one date per week, and the every_4_weeks generates every 4th week, starting with the 4th week after dtstart. That gives you a 3-on 1-off schedule.

i want to generate consecutive dates

I have a code like this:
import time
from datetime import date
startyear = raw_input("start year: ")
startmonth = raw_input("start month: ")
startday = raw_input("start day: ")
endyear = raw_input("end year: ")
endmonth = raw_input("end month: ")
endday = raw_input("end day: ")
startdate = date(int(startyear), int(startmonth), int(startday))
while startdate < date(int(endyear), int(endmonth), int(endday)):
print startdate
startdate = startdate.replace(day=startdate.day + 1)
what this code does is:
1.get start and end date by manual input
2.generate a list of dates between them
but the problem is, if I set up the date like, for example,
startdate: 2012-10-28
enddate: 2012-11-4
the output would be like:
2012-10-28
2012-10-29
2012-10-30
2012-10-31
ValueError: day is out of range for month
I want the output to be like:
2012-10-28
2012-10-29
2012-10-30
2012-10-31
2012-11-01
2012-11-02
2012-11-03
2012-11-04
So I want the dates to go through month.
any suggestions? any help would be really great.
Instead of startdate.replace(), you should use timedelta...
from datetime import date, timedelta
# Get input here...
while startdate < date(int(endyear), int(endmonth), int(endday)):
print startdate
startdate += timedelta(days=1)
There's a module called python-dateutil which is very useful for this kind of thing:
from datetime import datetime
from dateutil.rrule import rrule, DAILY
list(rrule(DAILY, dtstart=datetime(2012, 11, 24), until=datetime(2012,11,30)))
# [datetime.datetime(2012, 11, 24, 0, 0), datetime.datetime(2012, 11, 25, 0, 0), datetime.datetime(2012, 11, 26, 0, 0), datetime.datetime(2012, 11, 27, 0, 0), datetime.datetime(2012, 11, 28, 0, 0), datetime.datetime(2012, 11, 29, 0, 0), datetime.datetime(2012, 11, 30, 0, 0)]
And has other options, for instance, by days of the week (useful to only include MO, TU, WE, TH, FR for working weeks):
from dateutil.rrule import MO, WE
list(rrule(DAILY, byweekday=[MO, WE], dtstart=datetime(2012, 11, 24), until=datetime(2012,11,30))
# [datetime.datetime(2012, 11, 26, 0, 0), datetime.datetime(2012, 11, 28, 0, 0)]

Categories

Resources