In Python, how to display current time in readable format

PythonDatetimeTime

Python Problem Overview


How can I display the current time as:

12:18PM EST on Oct 18, 2010

in Python. Thanks.

Python Solutions


Solution 1 - Python

First the quick and dirty way, and second the precise way (recognizing daylight's savings or not).

import time
time.ctime() # 'Mon Oct 18 13:35:29 2010'
time.strftime('%l:%M%p %Z on %b %d, %Y') # ' 1:36PM EDT on Oct 18, 2010'
time.strftime('%l:%M%p %z on %b %d, %Y') # ' 1:36PM EST on Oct 18, 2010'

Solution 2 - Python

All you need is in the documentation.

import time
time.strftime('%X %x %Z')
'16:08:12 05/08/03 AEST'

Solution 3 - Python

import time
time.strftime('%H:%M%p %Z on %b %d, %Y')

This may come in handy

Solution 4 - Python

By using this code, you'll get your live time zone.

import datetime
now = datetime.datetime.now()
print ("Current date and time : ")
print (now.strftime("%Y-%m-%d %H:%M:%S"))

Solution 5 - Python

You could do something like:

>>> from time import gmtime, strftime
>>> strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
'Thu, 28 Jun 2001 14:17:15 +0000'

The full doc on the % codes are at http://docs.python.org/library/time.html

Solution 6 - Python

Take a look at the facilities provided by the time module

You have several conversion functions there.

Edit: see the datetime module for more OOP-like solutions. The time library linked above is kinda imperative.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionensnareView Question on Stackoverflow
Solution 1 - Pythondr jimbobView Answer on Stackoverflow
Solution 2 - PythonAifView Answer on Stackoverflow
Solution 3 - PythonBill ReasonView Answer on Stackoverflow
Solution 4 - PythonMuneer AhmadView Answer on Stackoverflow
Solution 5 - PythonThomas AhleView Answer on Stackoverflow
Solution 6 - PythonslezicaView Answer on Stackoverflow