Reputation: 16264
What is the fastest Python's equivalent to C#'s DateTime.Now.ToString("h tt")
?
If the time is now 0905h, it will give 9 AM
. If the time is now 1526h it will give 3 PM
. (Assuming some English locale.)
I have seen a number of examples but they are too long.
Upvotes: 1
Views: 35
Reputation: 33701
You could just use strftime
either from time
:
In [4]: import time
In [5]: time.strftime("%I %p")
Out[5]: '10 AM'
Or datetime
objects:
In [14]: from datetime import datetime
In [15]: today = datetime.now()
In [16]: today
Out[16]: datetime.datetime(2014, 11, 17, 10, 32, 10, 161824)
In [17]: today.strftime("%I %p")
Out[17]: '10 AM'
Upvotes: 3