I'm trying to change default output formatting of strptime and datetime in my functions:
def start_week_date(year, week):
format_string = "%i %i 1" % (int(year),int(week))
start = time.strptime(format_string, '%Y %W %w')
print datetime.date(start.tm_year, start.tm_mon, start.tm_mday)
return datetime.date(start.tm_year, start.tm_mon, start.tm_mday)
output of which is being passed to another one:
for date_oncall in date_range(start_week_date(year,week), start_week_date(year,week+1)):
print date_oncall
def date_range(start_date, end_date):
"""Generator of dates in between"""
if start_date > end_date:
raise ValueError("Start date is before end date.")
while True:
yield start_date
start_date = start_date + datetime.timedelta(days=1)
if start_date >= end_date:
break
Is there an elegant way to change default formatting so if day of a month or a month is < 10 it doesn't get the '0' at the beginning?
Basically instead of '03-05-2012' I would like to get '3-5-2012'.
Thanks much in advance for any suggestions.
Regards,
Jakub
date objects have a method, strftime, to manually specify the format, but it doesn't have an option to do what you want - so, that means you need to construct the string yourself from the other attributes of date_oncall. The good news is that this is quite easy:
>>> '{d.day}-{d.month}-{d.year}'.format(d=date_oncall)
'17-1-2010'
check this link:
http://www.tutorialspoint.com/python/time_strftime.htm
by using
time.strftime(string[, format])
you can specify the day of the month format without the '0' at the beginning, by using '%e'.
Related
I'm not a Python developer but have to fix an existing code.
In this code, a method (extract) is called providing an interval of dates:
extract(start_date, end_date)
The parameters can have for exemple the values:
start_date : 2020-10-01
end_date : 2022-01-03
The problem
The issue with this call is that the extract method only support a 1 year max interval of dates. If greater, the interval must be split, for exemple as follow:
extract('2020-10-01', '2020-12-31')
extract('2021-01-01', '2021-12-31')
extract('2022-01-01', '2022-01-03')
So I'm trying to create loop where the start_date and end_date are computed dynamically. But being new to Python, I have no ideas for now how this can be done. Any help would be greatly appreciated.
EDIT
Answer to some comments here
Tried so far finding a solution starting from code like this so far:
from datetime import datetime
from dateutil import relativedelta
from datetime import datetime
from_date = datetime.strptime('2020-10-01', "%Y-%m-%d")
end_date = datetime.strptime('2022-01-03', "%Y-%m-%d")
# Get the interval between the two dates
diff = relativedelta.relativedelta(end_date, from_date)
Then I thought iterating accross the years using diff.years and adding some logic to build the start_date and end_date from there, but I thought there might be a much simplier approach.
Also saw others possibilities like here but still no final simple result found at the moment.
from_str = '2020-10-01'
end_str = '2022-01-03'
from_year = int(from_str[:4])
end_year = int(end_str[:4])
if from_year != end_year:
# from_date to end of first year
extract(from_str, f"{from_year}-12-31")
# full years
for y in range(from_year + 1, end_year):
extract(f"{y}-01-01", f"{y}-12-31")
# rest
extract(f"{end_year}-01-01", end_str)
else:
extract(from_str, end_str)
As mentioned in the comments, you can either use the datetime library or you can also use pandas if you want. The pandas version is the following (admittively not the most pretty, but it does the job):
import pandas as pd
import datetime
start = datetime.datetime(2020,10,1)
end = datetime.datetime(2022,1,3)
def extract(from_dt, to_dt):
print(f'Extracting from {from_dt} to {to_dt}')
prev_end = pd.to_datetime(start)
for next_end in pd.date_range(datetime.datetime(start.year, 12, 31), end, freq='y'):
if next_end < end:
extract(prev_end.strftime('%Y-%m-%d'), next_end.strftime('%Y-%m-%d'))
else:
extract(prev_end.strftime('%Y-%m-%d'), end.strftime('%Y-%m-%d'))
prev_end = next_end + datetime.timedelta(days=1)
if prev_end < end:
extract(prev_end.strftime('%Y-%m-%d'), end.strftime('%Y-%m-%d'))
If you need to parse the original dates from strings, check out datetime.strptime
This kind of problems are nice ones to resolve by recursion:
from datetime import datetime
start_date = '2020-10-01'
end_date = '2022-01-03'
def intervalcalc(datestart,dateend):
newdate=dateend[:4] + '-01-01'
startd = datetime.strptime(datestart, "%Y-%m-%d")
endd = datetime.strptime(newdate, "%Y-%m-%d")
if endd < startd:
print(datestart, dateend)
return True
else:
print(newdate, dateend)
previousyear=str(int(newdate[:4])-1) + '-12-31'
intervalcalc(datestart,previousyear)
intervalcalc(start_date, end_date)
output:
2022-01-01 2022-01-03
2021-01-01 2021-12-31
2020-10-01 2020-12-31
You just need to change the prints by calls to extract function.
As mentioned by #Wups the conversion to date is not really necessary, it could be an string compare as they are YYYYMMDD dates.
Also, this can be done the other way around and calculate from the start date year + '-12-31' and then compare dateend>end_date to determine the anchor for the recursion.
I am new to functions and I am trying to write a function that returns the number of days between two dates:
My attempt:
import datetime
from dateutil.parser import parse
def get_x_days_ago (date_from, current_date = None):
td = current_date - parse(date_from)
if current_date is None:
current_date = datetime.datetime.today()
else:
current_date = datetime.datetime.strptime(date_from, "%Y-%m-%d")
return td.days
print(get_x_days_ago(date_from="2021-04-10", current_date="2021-04-11"))
Expected outcome in days:
1
So there seem to be multiple issues, and as I said in the comments, a good idea would be to separate the parsing and the logic.
def get_x_days_ago(date_from, current_date = None):
if current_date is None:
current_date = datetime.datetime.today()
return (current_date - date_from).days
# Some other code, depending on where you are getting the dates from.
# Using the correct data types as the input to the get_x_days_ago (datetime.date in this case) will avoid
# polluting the actual logic with the parsing/formatting.
# If it's a web framework, convert to dates in the View, if it's CLI, convert in the CLI handling code
date_from = parse('April 11th 2020')
date_to = None # or parse('April 10th 2020')
days = get_x_days_ago(date_from, date_to)
print(days)
The error you get is from this line (as you should see in the traceback)
td = current_date - parse(date_from)
Since current_date="2021-04-11" (string), but date_from is parsed parse(date_from), you are trying to subtract date from the str.
P.S. If you have neither web nor cli, you can put this parsing code into def main, or any other point in code where you first get the initial strings representing the dates.
It looks like you're already aware that you can subtract a datetime from a datetime. I think, perhaps, you're really looking for this:
https://stackoverflow.com/a/23581184/2649560
I am trying to get user input for 2 different dates which i will pass on to another function.
def twodifferentdates():
print("Data between 2 different dates")
start_date = datetime.strptime(input('Enter Start Date in m/d/y format'), '%m&d&Y')
end_date = datetime.strptime(input('Enter end date in m/d/y format'), '%m&d&Y')
print(start_date)
twodifferentdates()
I have tried a lot of different ways to enter the dates but i keep getting
ValueError: time data '01/11/1987' does not match format '%m&d&Y'
I have used the same code which was discussed in:
how do I take input in the date time format?
Any help here would be appreciated.
Replace %m&d&Y with %m/%d/%Y as described in the referenced post.
datetime.strptime() requires you to specify the format, on a character-by-character basis, of the date you want to input. For the string '01/11/1987' you'd do
datetime.strptime(..., '%m/%d/%Y')
where %m is "two-digit month", %d is "two-digit day" and %Y is "four-digit year" (as opposed to two-digit year %y. These values are separated by slashes.
See also the datetime documentation which describes how to use strptime and strftime.
I'm not very experienced with the datetime module, but the error seems to be the way you're taking input. You should be taking it like this:
start_date = datetime.strptime(input('Enter Start Date in m/d/y format'), '%m &d &Y')
or
start_date = datetime.strptime(input('Enter Start Date in m/d/y format'), '%m/&d/&Y')
To get date I use this block:
currentDate = date.today()
today = currentDate.strftime('%m/%d/%Y')
It returns me this format 12/22/2014 or 01/02/2015
Then I have to compare to string from the file (note: I can't change the string) 12/22/2014 or 1/2/2015 and I use:
if l[0] == today:
In second case it obviously failed.
My question: how could I change strftime() in order to return only one charachter for month and day when it has preceeding zero?
Referring to the documentation, it doesn't appear that there is a character sequence for this. However, you could correct the result as follows:
today = currentDate.strftime('%m/%d/%Y').replace("/0", "/")
if today[0] == '0':
today = today[1:]
This will eliminate any leading 0s so long as the values are split with a forward slash.
just compare datetime objects:
from datetime import datetime, date
currentDate = date.today()
file_dt = "1/3/2015"
dt2 = datetime.strptime(file_dt,"%m/%d/%Y")
print(dt2.date() == currentDate)
today = currentDate.strftime('%-m/%-d/%Y')
WARNING, not standard, so could not work on some platforms (check strftime(3) documentation, section "Glibc notes"). Anyway, I agree with other answers, better to compare datetime objects
I have the following simple setup, where fromDate and toDate are strings on the format "YYYY-MM-DD":
class SomeType(Base):
date = Column(DateTime)
def findAll(fromDate, toDate):
return session.query(SomeType).filter(SomeType.date >= fromDate, SomeType.date <= toDate).all()
The problem is that it doesn't find what I want it to find unless I modify the input dates like this:
def findAll(fromDate, toDate):
fromDate = fromDate + " 00:00"
toDate = toDate + " 24:00"
return session.query(SomeType).filter(SomeType.date >= fromDate, SomeType.date <= toDate).all()
But that doesn't look good. Any ideas on how I can do this the right way?
How about using datetime.datetime objects instead of strings for fromDate, toDate?
from datetime import datetime, timedelta
def findAll(fromDate, toDate):
fromDate = datetime.strptime(fromDate, '%Y-%m-%d')
toDate = datetime.strptime(toDate, '%Y-%m-%d') + timedelta(days=1)
return session.query(SomeType).filter(
SomeType.date >= fromDate,
SomeType.date < toDate).all()
The problem is that your SomeType.date column is not simple date, but is datetime column, so it contains also a time component.
This type mismatch is the cause of your problem. If this is the case then following should work:
session.query(SomeType).filter(func.date(SomeType.date) >= fromDate, func.date(SomeType.date) <= toDate).all()
where we basically cast datetime to date using DATE(...) function of MySql.
However, I would probably also prefer working with date(time) data types instead of strings. You are just lucky that most databases implicitly allow parsing of ISO-compliant string representations of DATEs.
I know this is old, but while trying to find my answer, I found datetime.combine
you can do
select(SomeTable)
.filter( SomeTable.datetime_issued >= datetime.combine(start_date, time.min),
SomeTable.datetime_issued <= datetime.combine(end_date, time.max))
datetime.combine will combine date and time into datetime
https://docs.python.org/3/library/datetime.html#datetime.datetime.combine
When combining, you should use time.min, time.max which will give you min and max time
print(combine(date.today(), time.min), combine(date.today(), time.max))
This will print
2022-10-14 00:00:00, 2022-10-14 23:59:59.999999
https://docs.python.org/3/library/datetime.html#datetime.time.max