Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Python, how to display current time in readable format

How can I display the current time as:

12:18PM EST on Oct 18, 2010 

in Python. Thanks.

like image 678
ensnare Avatar asked Oct 18 '10 17:10

ensnare


People also ask

Which function gives the human readable time in Python?

Python's time module provides a function for getting local time from the number of seconds elapsed since the epoch called localtime() .


2 Answers

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' 
like image 93
dr jimbob Avatar answered Sep 21 '22 21:09

dr jimbob


All you need is in the documentation.

import time time.strftime('%X %x %Z') '16:08:12 05/08/03 AEST' 
like image 20
Aif Avatar answered Sep 20 '22 21:09

Aif