Python Forum

Full Version: Python DateTime is broken
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Please help me fix this, when I tried this
import datetime
print(datetime.datetime(2019, 10, 10).strftime('%d/%m/%Y'));
It outputs the wrong result.
It should out the the European date format (
Quote:10/10/2019
) but instead it outputs the American date format (
Quote:10/10/2019
).
Plz fix this, this is frustrating me since 10/10/2019.
How do I make it so it outputs the European date? (10/10/2019)
What? Those outputs are literally the same. In any case, I don't believe you when you say it's broken, not least because if it was someone would have noticed by now. An example where the month and day are different:

$ python3
Python 3.6.2 (default, Oct  5 2017, 12:21:44) 
[GCC 5.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> datetime.datetime(2019, 11, 10).strftime('%d/%m/%Y')
'10/11/2019'
>>>
In my case it works fine - output is "10/10/2019" in European date format. There is probably a virus in your computer.
(Dec-31-2019, 06:22 PM)ndc85430 Wrote: [ -> ]What? Those outputs are literally the same. In any case, I don't believe you when you say it's broken, not least because if it was someone would have noticed by now. An example where the month and day are different:

$ python3
Python 3.6.2 (default, Oct  5 2017, 12:21:44) 
[GCC 5.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> datetime.datetime(2019, 11, 10).strftime('%d/%m/%Y')
'10/11/2019'
>>>
Yeah, that's the problem doing
datetime.datetime(2019, 11, 10).strftime('%d/%m/%Y')
returns October 11, 2019 instead of 11 October 2019. But this:
datetime.datetime(2019, 31, 12).strftime('%d/%m/%Y')
should return 31/12/2019 instead of throwing an error.
It's interesting that datetime only isn't broken when the day and the month syntaxes are the same. It's a coincidence that I had this problem on 10/10, thus it shows the same thing and I didn't realise this.
Any solution, it seems that only does what I wanted the datetime module to do on 1/1, 2/2, 3/3, 4/4, 5/5, 6/6, 7/7, 8/8, 9/9, 10/10, 11/11 and 12/12
I don't think you understand. You can use strftime to make a text representation of the date in whatever format you want. But the datetime object (datetime.datetime(2019, 11, 10)), must be specified in year, month, day order, or you must clarify with keyword arguments what you want (datetime.datetime(year = 2019, day = 11, month = 10)).
(Jan-02-2020, 03:09 AM)ichabod801 Wrote: [ -> ]I don't think you understand. You can use strftime to make a text representation of the date in whatever format you want. But the datetime object (datetime.datetime(2019, 11, 10)), must be specified in year, month, day order, or you must clarify with keyword arguments what you want (datetime.datetime(year = 2019, day = 11, month = 10)).

Why is it in year month day order? TIL Python dates are in the same order as the date in Japan.

In my country, it's either year day month or day month year.
I don't know why. You'd have to ask Guido. I expect it's because the tuple of (year, month, day) sorts correctly without having to know it's a date.
ISO8601 uses year/month/day, as I tend to, because as ichabod mentioned it sorts naturally. So +1 to the guess that that was the designer's thinking.
(Jan-02-2020, 11:07 PM)10OctNotOct10a1 Wrote: [ -> ]Why is it in year month day order? TIL Python dates are in the same order as the date in Japan.

In my country, it's either year day month or day month year.
Maybe is something that common to use in Japan,but official national standards is JIS X 0301.
ISO 8601
Quote:Extensions according to national standards
There is Japanese Industrial Standard JIS X 0301 (former JIS C 6262), and the translation of ISO 8601 is JIS X 0301
The date is represented in the basic format "YY.MM.DD"
(for example, the year, month, and day are separated by a hyphen , such as 2019-06-23
If want more and better option for standard formats or time-zones use Pendulum
>>> import pendulum
>>> 
>>> now = pendulum.now('Asia/Tokyo')
>>> now
DateTime(2020, 1, 3, 16, 36, 25, 399457, tzinfo=Timezone('Asia/Tokyo'))
>>> print(now)
2020-01-03T16:36:25.399457+09:00

>>> # Example of different formats
>>> print(now.to_rfc2822_string())
Fri, 03 Jan 2020 16:36:25 +0900

>>> print(now.to_rfc850_string())
Friday, 03-Jan-20 16:36:25 JST

>>> print(now.to_rfc3339_string())
2020-01-03T16:36:25.399457+09:00

>>> print(now.to_w3c_string())
2020-01-03T16:36:25+09:00

>>> now.to_iso8601_string()
'2020-01-03T16:36:25.399457+09:00'