Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare two Datetime field in Django

Tags:

django

I have used datetime.datetime.now() for storing datefield in my model which is saved as 2016-06-27 15:21:17.248951+05:30. Now I want to compare the datefield with the datetime value getting from the frontend, like Thu May 26 2016 00:00:00 GMT 0530 (IST). How should Django query the model to compare both datefields?

# models.py

datefield = models.DateTimeField(blank=True,null=True)

I have tried converting datefield getting from frontend by using split() and remove() function of Python to create it in format as 2016-06-27 13:25:35.

But still no solution and getting Null even I am comparing same date value like this (2016-06-27 13:25:35) value with this (2016-06-27 12:36:34.898593+00) value, as date in both values are same.

I have checked it using simple Django query as follows:

company_details.objects.filter(datefield=datefield).only('par1','par2',...)
like image 792
Piyush S. Wanare Avatar asked Jun 28 '16 06:06

Piyush S. Wanare


People also ask

What is datetimefield in Django?

DateTimeField – Django Models. DateTimeField is a date and time field which stores date, represented in Python by a datetime.datetime instance. As the name suggests, this field is used to store an object of datetime created in python. The default form widget for this field is a TextInput.

What is the difference between date and DateTime fields in Salesforce?

The difference between these fields arises on the format of the data being stored. There are options to store only date value, even time value alone can be capture. As per datetime field both date and time values will be captured.

What is the use of datetimefield?

Last Updated : 12 Feb, 2020 DateTimeField is a date and time field which stores date, represented in Python by a datetime.datetime instance. As the name suggests, this field is used to store an object of datetime created in python. The default form widget for this field is a TextInput.

How to get the current date of a datetime field?

Use datetime.now () (notice the parens). Other than that, remember that the field will always be a datetime object. Also, (I guess that) you should check only the date of the datetime to match the current date (or else it will only match that specific second).


2 Answers

Once you have converted the date from the front end into a ISO format like 2016-06-27 13:25:35, you can use it to query the model with one of these

Model.objects.filter(date_created__startswith=today)
Model.objects.filter(date_created__contains=today)

It works too if you are only looking to filter for a certain date like 2016-06-27.

Model.objects.filter(date_created__startswith=date(2016, 6, 27))

To parse the date string you are getting from the frontend, there are two options.

If your date string is well-formatted, you can simply parse it with for example datestring.strftime('%Y-%m-%d %H:%M:%S') to get a datetime() object.

If your date string is unreliable and may be formatted in different ways that you can't predict, I would recommend to using DateUtil. It comes with a parser that will try to convert any string into a datetime, for example

>>> from dateutil.parser import parse
>>> parse("Today is January 1, 2047 at 8:21:00AM", fuzzy_with_tokens=True)
(datetime.datetime(2011, 1, 1, 8, 21), (u'Today is ', u' ', u'at '))
like image 176
C14L Avatar answered Oct 06 '22 12:10

C14L


Compare the date and time like this. Just give the variable like this '%Y-%m-%d %H:%M:%S' as much you want.

In [1]: from datetime import datetime
In [2]: past = datetime.now()
In [3]: present = datetime.now()
In [4]: present.strftime('%Y-%m-%d %H:%M:%S') == past.strftime('%Y-%m-%d %H:%M:%S')
Out[17]: False
In [5]: present.strftime('%Y-%m-%d %H:%M') == past.strftime('%Y-%m-%d %H:%M')
Out[2]: True

If you want to compare only the date use like this.

if present.date() == past.date():
    #Do your stuff
like image 39
Rahul K P Avatar answered Oct 06 '22 10:10

Rahul K P