How do I get the current time?
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 2 Looping
--
Chapters
00:00 Question
00:15 Accepted answer (Score 3742)
00:51 Answer 2 (Score 1156)
01:05 Answer 3 (Score 827)
01:25 Answer 4 (Score 517)
01:46 Thank you
--
Full question
https://stackoverflow.com/questions/4155...
Accepted answer links:
[datetime]: https://docs.python.org/3/library/dateti...
[datetime]: https://docs.python.org/3/library/dateti...
Answer 2 links:
[time.strftime()]: http://docs.python.org/3.3/library/time....
Answer 3 links:
[strftime]: https://docs.python.org/library/datetime...
Answer 4 links:
[Harley's answer]: https://stackoverflow.com/questions/4155...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #datetime #time
#avk47
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 2 Looping
--
Chapters
00:00 Question
00:15 Accepted answer (Score 3742)
00:51 Answer 2 (Score 1156)
01:05 Answer 3 (Score 827)
01:25 Answer 4 (Score 517)
01:46 Thank you
--
Full question
https://stackoverflow.com/questions/4155...
Accepted answer links:
[datetime]: https://docs.python.org/3/library/dateti...
[datetime]: https://docs.python.org/3/library/dateti...
Answer 2 links:
[time.strftime()]: http://docs.python.org/3.3/library/time....
Answer 3 links:
[strftime]: https://docs.python.org/library/datetime...
Answer 4 links:
[Harley's answer]: https://stackoverflow.com/questions/4155...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #datetime #time
#avk47
ACCEPTED ANSWER
Score 3980
Use datetime:
>>> import datetime
>>> now = datetime.datetime.now()
>>> now
datetime.datetime(2009, 1, 6, 15, 8, 24, 78915)
>>> print(now)
2009-01-06 15:08:24.789150
For just the clock time without the date:
>>> now.time()
datetime.time(15, 8, 24, 78915)
>>> print(now.time())
15:08:24.789150
To save typing, you can import the datetime object from the datetime module:
>>> from datetime import datetime
Then remove the prefix datetime. from all of the above.
ANSWER 2
Score 1230
Use time.strftime():
>>> from time import gmtime, strftime
>>> strftime("%Y-%m-%d %H:%M:%S", gmtime())
'2009-01-05 22:14:39'
ANSWER 3
Score 883
from datetime import datetime
datetime.now().strftime('%Y-%m-%d %H:%M:%S')
Example output: '2013-09-18 11:16:32'
See list of strftime directives.
ANSWER 4
Score 539
Similar to Harley's answer, but use the str() function for a quick-n-dirty, slightly more human readable format:
>>> from datetime import datetime
>>> str(datetime.now())
'2011-05-03 17:45:35.177000'