Python 3 printing datetime date as 1900?
For some reason, when I format datetime.datetime.now()
to print the date and time nicely in Python 3, the date keeps coming out as 01/01/1900.
Here is my code:
print('{}'.format(str(datetime.datetime.now().time().strftime('%x %X'))))
Output: 01/01/00 13:40:11
Any ideas on why the date is wrong?
Solution 1:
Converting to a datetime.time
removes the date:
Code:
print(dt.datetime.now().time().strftime('%x %X'))
print(dt.datetime.now().strftime('%x %X'))
Results:
01/01/00 20:47:41
05/05/17 20:47:41
Solution 2:
Your call is to datetime.datetime.now().time()
Notice the difference between that, and datetime.datetime.now()
>>> datetime.datetime.now().time()
datetime.time(22, 45, 48, 610362)
>>> datetime.datetime.now()
datetime.datetime(2017, 5, 5, 22, 45, 57, 874420)
You want to remove the .time()
portion in your .format()
.
>>> print('{}'.format(datetime.datetime.now().strftime('%x %X')))
05/05/17 22:47:30