Comparing two dates in Django [duplicate] - python

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
How to compare datetime in Django?
I need to compare 2 dates
object.submit_date.ctime() > user.last_login.ctime()
but always get false.
no matter whether last_login it is after the last submit_date

are comparing wrong, you have to use date() or isoformat() instead of ctime()
Like this:
object.submit_date.isoformat() > user.last_login.isoformat()
this includes the time
or
object.submit_date.date() > user.last_login.date()

If its a datetime object. you can simply compare the datetime ojects rather than using ctime.
>>> a =datetime.now()
>>> b = datetime.now()
>>> a>b
False
>>> b>a
True

Related

Can anyone please explain why set is behaving like this with boolean in it? [duplicate]

This question already has an answer here:
Python set interpetation of 1 and True
(1 answer)
Closed 1 year ago.
Please explain the behavior of the set in the image. I know that set is unordered but where are the other elements from the set a & b ?
True and 1 are the same:
>>> True == 1
True
>>>
Since sets can't have duplicate values, it only takes the one that appears first.
You can see that if you convert True to int:
>>> int(True)
1
>>>
The output is 1.

Comparing Time - Python [duplicate]

This question already has answers here:
How do I check the difference, in seconds, between two dates?
(7 answers)
Closed 4 years ago.
I have a function where I read the time from a file. I put this into a variable. I then subtract this value from the current time which most of the time will give me a value around .
My problem is im not sure how to check if this value which I attach to a variable is greater than say 20 seconds.
def numberchecker():
with open('timelog.txt', 'r') as myfile:
data=myfile.read().replace('\n','')
a = datetime.strptime(data,'%Y-%m-%d %H:%M:%S.%f')
b = datetime.now()
c = b-a (This outputs for example: 0:00:16.657538)
d = (would like this to a number I set for example 25 seconds)
if c > d:
print ("blah blah")
The difference which you're getting is a timedelta object.
You can just use c.seconds to get the seconds.
if c.total_seconds() > datetime.timedelta(seconds=d):
print ("blah blah")

Python Datetime Formatted as "1/1/1990" [duplicate]

This question already has answers here:
Python strftime - date without leading 0?
(21 answers)
How to convert a time to a string
(4 answers)
Closed 9 years ago.
In python how would I format the date as 1/1/1990?
dayToday = datetime.date(1990,1,1)
print dayToday
This returns 1990-01-01, but I want it to look like 1/1/1990. (Jan 1 1990)
Try to look into python datetime.strftime
dayToday = datetime.date(1990,1,1)
print dayToday.strftime('%Y/%m/%d')
>>> 1990/01/01
print dayToday.strftime('%Y/%b/%d')
>>> 1990/Jan/01
Use the datetime.strftime function with an appropriate format string:
>>> now = datetime.datetime.now()
>>> print now.strftime('%Y/%m/%d')
2013/04/19
Others have showed how to get the output 1990/01/01, but assuming you don't want the leading zeros in there, the only way that I know of to do it is to do the string formatting yourself:
>>> '{dt.year}/{dt.month}/{dt.day}'.format(dt = dt.datetime.now())
'2013/4/19'
With the correct format and a without leading 0:
>>> import datetime
>>> now = datetime.datetime.now()
>>> now.strftime("%-m/%-d/%Y")
'4/19/2013'
Reported to only work for Linux, but I haven't tested anything else personally.
Tested and working for 2.7.3 and 3.2.3 on Linux x64.

Remove leading 0's for str(date) [duplicate]

This question already has answers here:
Python strftime - date without leading 0?
(21 answers)
Closed 5 years ago.
If I have a datetime object, how would I get the date as a string in the following format:
1/27/1982 # it cannot be 01/27/1982 as there can't be leading 0's
The current way I'm doing it is doing a .replace for all the digits (01, 02, 03, etc...) but this seems very inefficient and cumbersome. What would be a better way to accomplish this?
You could format it yourself instead of using strftime:
'{0}/{1}/{2}'.format(d.month, d.day, d.year) // Python 2.6+
'%d/%d/%d' % (d.month, d.day, d.year)
The datetime object has a method strftime(). This would give you more flexibility to use the in-built format strings.
http://docs.python.org/library/datetime.html#strftime-and-strptime-behavior.
I have used lstrip('0') to remove the leading zero.
>>> d = datetime.datetime(1982, 1, 27)
>>> d.strftime("%m/%d/%y")
'01/27/82'
>>> d.strftime("%m/%d/%Y")
'01/27/1982'
>>> d.strftime("%m/%d/%Y").lstrip('0')
'1/27/1982'

Python: How to convert datetime format? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to convert a time to a string
I have a variable as shown in the below code.
a = "2011-06-09"
Using python, how to convert it to the following format?
"Jun 09,2011"
>>> import datetime
>>> d = datetime.datetime.strptime('2011-06-09', '%Y-%m-%d')
>>> d.strftime('%b %d,%Y')
'Jun 09,2011'
In pre-2.5 Python, you can replace datetime.strptime with time.strptime, like so (untested): datetime.datetime(*(time.strptime('2011-06-09', '%Y-%m-%d')[0:6]))
#Tim's answer only does half the work -- that gets it into a datetime.datetime object.
To get it into the string format you require, you use datetime.strftime:
print(datetime.strftime('%b %d,%Y'))

Categories