datetime.datetime.now() in military time

Mike Driscoll kyosohma at gmail.com
Tue Aug 19 12:09:54 EDT 2008


On Aug 19, 10:42 am, korean_dave <davidrey... at gmail.com> wrote:
> import datetime
>
> def main():
>     timenow = datetime.datetime.now()
>     print(str(timenow.hour))
>
> if __name__ == "__main__":
>     main()
>
> If the time was, say, 2pm, how can I make output of timenow.hour "14"
> instead of "2"?
>
> Thanks.
> -Dave

Use the strftime() method to convert the time. Something like this:

print timenow.strftime('%H')

See also: http://docs.python.org/lib/module-time.html

Mike



More information about the Python-list mailing list