How to calculate the number of days remaining for another date - python

How to import a date from view and calculate the number of days remaining for another date?
Here is the solution I tried but it's not working:
class saisir_soumi(osv.osv):
_name='saisir.soumi'
def compa_date(self,cr,uid,ids,args,fields,context=None):
r = {}
date_format = "%D/%M/%Y"
joining_date = 'date_depot'
current_date = '29/04/2016 02:02:02'# current date
d1 = datetime.strptime(joining_date, date_format)
d2 = datetime.strptime(current_date, date_format)
diff = current_date - datetime.date.today()
return diff.days
_rec_name = 'NumOffre'
_columns = {
'NumOffre' : fields.char('N° Offre',required=True),
'organisme_s' : fields.char('Organisme',required=True),
'date_depot' : fields.datetime('Date dépot de soumission'), # the date to be seized and used for its difference with current date( today)
'jrestant': fields.function(compa_date,string='Jours restant')
}
_sql_constraints = [
('uniq_NumOffre', 'unique(NumOffre,id)', "numero offre doit resté unique !"),
]

you have to do it properly:
specify correct date/time format
parse datetime from string
substract the same data types: datetime - datetime
Code:
In [68]: current_date = '29/04/2016 02:02:02'
In [69]: date_format = '%d/%m/%Y %H:%M:%S'
In [70]: (datetime.datetime.strptime(current_date, date_format) - datetime.datetime.now()).days
Out[70]: 5

Related

How to return values only within a specific date range?

I have a program that scrapes through an API and gets the required values from the fields. There is a field called published_date one act json object. I want to publish only the values for the last 2 months from current date.
try:
price = str(price).replace(',', '')
price = Decimal(price)
if date < end:
if not math.isnan(price):
report_item = PriceItem(
source=SOURCE,
source_url=crawled_url,
original_index_id=original_index_id,
index_specification=index_specification,
published_date=date,
price=price.quantize(Decimal('1.00'))
)
yield report_item
except DecimalException as ex:
self.logger.error(f"Non decimal price of {price} "
f"found in {original_index_id}", exc_info=ex)
The published date is extracted:
for report_date in REPORT_DATE_TYPES:
if report_date in result:
date = result[report_date].split(' ')[0]
date = datetime.strptime(date, '%m/%d/%Y')
MAX_REPORT_MONTHS = 3
current_date = datetime.now()
current_date_str = current_date.strftime('%m/%d/%Y')
start = datetime.strptime(current_date_str, '%m/%d/%Y')
last_date = current_date - relativedelta(months=MAX_REPORT_MONTHS)
last_date_str = last_date.strftime('%m/%d/%Y')
end = datetime.strptime(last_date_str, '%m/%d/%Y')
The above I say last date string and current date string.
Extract of the api:
After having gathered the data into a dataframe you can convert the column containing the dates to datetime and then through comparison operators mantain just the desidered data.
For example, assuming this is your data:
data = {'date': ['02/02/2022 10:23:23', '09/23/2021 10:23:23', '02/01/2021 10:23:23', '12/15/2021 10:23:23'], 'random': [324, 231, 213, 123]}
df = pd.DataFrame(data)
# convert date column to datetime
df['date'] = pd.to_datetime(df['date'], format="%m/%d/%Y %H:%M:%S")
# select "threshold" date, two months before current one
current_date = datetime.now()
last_date = current_date - relativedelta(months=2)
# select data published after last_date
df[df['date'] > last_date]
If we consider the date of today we will have this result.
Before:
date random
0 02/02/2022 10:23:23 324
1 09/23/2021 10:23:23 231
2 02/01/2021 10:23:23 213
3 12/15/2021 10:23:23 123
After:
date random
0 2022-02-02 10:23:23 324
3 2021-12-15 10:23:23 123

Why is my date in python wrong after formating using timedelta?

I am using Django and have a problem with a date that I need to calculate.
The Variable data > test should be 17:00 and not 15:00. Why does this happen as soon as I format the date?
My timezone is Europe/Berlin. Changing the timezone has to effect to the time printing in test. It is always -2h
def date(req):
now = timezone.now()
model = MyModel.objects.filter(date__gt=now).first()
next = model.date
future = timezone.timedelta(hours=float(model.future)) #model.future = 1.5
open = next-future
date = next.strftime('%Y/%m/%d')
data = {
'next': next,
'date': date,
'time': open.astimezone(timezone.utc).strftime('%Y-%m-%d %H:%M:%S.%f'),
'test': open.strftime('%Y/%m/%d %H:%M:%S%z')
}
What I get:
next: 20. November 2021 18:30
date: 2021/11/20
time: 2021-11-20 15:15:00.000000
test: 2021/11/20 15:00:00+0000
https://docs.djangoproject.com/en/3.2/topics/i18n/timezones/#naive-and-aware-datetime-objects
You should use:
from django.utils import timezone
now = timezone.now()
Datetime isn't time-zone aware.
You cut the timezone info (the offset +02:00) with .strftime(). You need to include it with %z.
In case you want to convert it to a time string with the offset already added.
open.astimezone(timezone.utc).strftime('%Y-%m-%d %H:%M:%S.%f')
This works for me.
from django.utils import timezone
def date(req):
now = timezone.now()
model = MyModel.objects.filter(date__gt=now).first()
next = model.date
future = timezone.timedelta(hours=float(model.future)) #model.future = 1.5
open = next-future
date = timezone.localtime(open)
data = {
'next': date.strftime('%Y/%m/%d %H:%M:%S'),
'date': date.strftime('%Y/%m/%d'),
'time': date.strftime('%H:%M:%S'),
}

How to add a 1 day to today's date in python?

What i'm trying to do is to add one extra day to today's date and have the outcome match this formula "%Y-%m-%d" and nothing else. I want the printed results to match this yyyy-mm-dd
from datetime import datetime, timedelta, date
s = date.today()
date = datetime.strptime(s, "%Y-%m-%d")
modified_date = date + timedelta(days=1)
datetime.strftime(modified_date, "%Y-%m-%d")
print(modified_date)
You are trying to do date operations on strings and not using the result of your formatting call:
s = date.today()
modified_date = s + timedelta(days=1)
modified_date = modified_date.strftime("%Y-%m-%d") # this would be more common
# modified_date = datetime.strftime(modified_date, "%Y-%m-%d")
print(modified_date)

Sphinx Search How can I search above the certain date

I have a code :
def set_date_range_filter(self,attribute = None,start_date = None , end_date = None):
if attribute is None:
return
#Make sure set the passing start date and end date
if not start_date or not end_date :
return
if isinstance(start_date, str) :
start_date = datetime.strptime(start_date, "%Y-%m-%d")
if isinstance(start_date, unicode) :
start_date = datetime.strptime(str(start_date), "%Y-%m-%d")
if isinstance(end_date ,str):
end_date = datetime.strptime(end_date, "%Y-%m-%d")
if isinstance(end_date ,unicode):
end_date = datetime.strptime(str(end_date), "%Y-%m-%d")
# Shphnx Range Filter ,start_date and end_date must be integers that define the acceptable attribute values range
start_date = int(time.mktime(start_date.timetuple()))
end_date = int(time.mktime(end_date.timetuple()))
if start_date > end_date :
return
self.sphinx.SetFilterRange(str(attribute),start_date,end_date)
I want to update this code to accept only 'start_date' or only 'end_date' or both.
Like i want all date from 2014-01-01 or i want all data after 2014-01-01
or say i want all data from 2014-01-01 to 2014-09-01. how can i archive this ??
Rather than
if not start_date or not end_date :
return
replace with say
if not start_date:
start_date = '1971-01-01'
if not end_date:
end_date = '2037-01-01'
Or similar. If either are missing, then just use a very early, or very late dates (outside the range of your data). Example dates above choosen within range of unix timestamps.
(will then be turned into proper date objects via strptime)

How to convert string date in datetime.date format

I want to convert date like Jun 28 in datetime format like 2014-06-28. I tried following code and many more variation which gives me correct output in ipython but I m unable to save the record in database. It throws error as value has an invalid date format. It must be in YYYY-MM-DD format. Can anyone help me to fix this issue ?
Following is the code snippet
m = "Jun"
d = 28
y = datetime.datetime.now().year
m = strptime(m,'%b').tm_mon
if m > datetime.datetime.now().month:
y=y-1
new_date = str(d)+" "+str(m)+" "+str(y)
new_date = datetime.datetime.strptime(new_date, '%b %d %Y').date()
my models.py is as
class Profile(models.Model):
Name = models.CharField(max_length = 256, null = True, blank = True)
Location = models.CharField(max_length = 256, null = True, blank = True)
Degree = models.CharField(max_length = 256, null = True, blank = True)
Updated_on = models.DateField(null = True, blank = True)
Code that saves to model is like
def save_record(self):
try:
record = Profile(Name= indeed.name,
Location = loc,
Degree = degree,
Updated_on = new_date,
)
record.save()
print "Record added"
except Exception as err:
print "Record not added ",err
pass
Thanks in advance
Once you have a date object, you can use the strftime() function to format it into a string.
Let's say new_date is your date object from your question. Then you can do:
new_date.strftime('%Y-%m-%d')
Btw, you can do the same with a datetime object too.
EDIT:
Double check whether your Updated_on field uses DateField or DateTimeField. That will affect whether you use a datetime.date() object or datetime.datetime() object, respectively.
I tried on console:
>>import datetime
>>datetime.datetime.strptime("Jun-08-2013", '%b-%d-%Y').date()
datetime.date(2013, 6, 8)
There are several errors in the code. So solution should be:
m = "Jun"
d = 28
if datetime.datetime.strptime("Aug",'%b').month > datetime.datetime.now().month:
y= (datetime.datetime.now() - relativedelta(years=1)).year #from dateutil.relativedelta import relativedelta
else:
y=datetime.datetime.now().year
new_date = str(m)+"-"+str(d)+"-"+str(y)
new_date = datetime.datetime.strptime(new_date, '%b-%d-%Y').date()
new_date is a date object, so it should be saved to models.DateField() without any problem(including format issues).

Categories

Resources