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.
Related
This question already has answers here:
Parse date string and change format
(10 answers)
Closed 3 years ago.
I realize this might be the most well-documented thing on the Internet but I can't seem to get it right. I have a string, '2019-10-16' that I want to turn into a date object so I can increase it incrementally, but can still be converted to the string '2019-10-06' again. However, I seem to only be able to get it as 2019.10.16 or something similar.
import datetime
day = '2019-10-16'
date_object = datetime.datetime.strptime(day, '%Y-%m-%d')
>date_object
>datetime.datetime(2019, 10, 16, 0, 0)
To change it use date_object.strftime('%Y-%m-%d')
This question already has answers here:
Parse date string and change format
(10 answers)
Closed 6 years ago.
I have strings that look like this: "16-Jul-8"
I want to convert them to something like: 16/07/08
datestr = datefields[0]+"-"+datefields[1]+"-"+datefields[2]
dt = datetime.datetime(datestr)
dt.strftime('%d-%m-%y')
I keep coming up empty...
it's so easy,
from datetime import datetime
function = lambda date_str: datetime.strptime(date_str, "%y-%b-%d").strftime("%y/%m/%d")
print function("16-Jul-8")
You can read python's documentations from here.
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'
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'))
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