Search code examples
pythoncpython

CPython - Making the date show up using the date on your computer


I'm really new to programming and I only just started using Python, if anyone could edit the code I put up to make it work how I want it to then please do.

I was wondering if I could make the date show up, on my python program but make it different for different regions, so if someone opened the program in United Kingdom the day would show up first and if it was in the US it would show the month or year first etc.

This is what I have so far.

import datetime
currentDate = datetime.date.today()
print(currentDate.strftime('Todays date is: %d %B %Y'))

I currently have it set so it shows the day first then the month then the year.

Is there anyway to make it use it in a different order depending on what country you're in?


Solution

  • Does this work for you?

    >>> import datetime
    >>> today = datetime.date.today()
    >>> print(today.strftime('%x'))
    09/10/15
    

    Specifically, you probably should look at the %c, %x, and %X format codes. See 8.1.8. strftime() and strptime() Behavior for more information on how to use the strftime method.