datetime - Comment obtenir l'heure actuelle dans Python

Mots clés : pythondatetimepython

meilleur 5 Réponses datetime - Comment obtenir l'heure actuelle dans Python

vote vote

90

>>> import datetime >>> datetime.datetime.now() datetime.datetime(2009, 1, 6, 15, 8, 24, 78915)  >>> print(datetime.datetime.now()) 2009-01-06 15:08:24.789150 
>>> datetime.datetime.now().time() datetime.time(15, 8, 24, 78915)  >>> print(datetime.datetime.now().time()) 15:08:24.789150 
>>> from datetime import datetime 
vote vote

86

>>> from time import gmtime, strftime >>> strftime("%Y-%m-%d %H:%M:%S", gmtime()) '2009-01-05 22:14:39' 
vote vote

78

from datetime import datetime datetime.now().strftime('%Y-%m-%d %H:%M:%S') 
vote vote

65

>>> from datetime import datetime >>> str(datetime.now()) '2011-05-03 17:45:35.177000' 
vote vote

57

import time 
>>> time.time() 1424233311.771502 
>>> time.ctime() 'Tue Feb 17 23:21:56 2015' 
>>> time.ctime(1424233311.771502) 'Tue Feb 17 23:21:51 2015' 
>>> import datetime 
>>> datetime.datetime.now() datetime.datetime(2015, 2, 17, 23, 43, 49, 94252) >>> print(datetime.datetime.now()) 2015-02-17 23:43:51.782461 
>>> datetime.datetime.utcnow() datetime.datetime(2015, 2, 18, 4, 53, 28, 394163) >>> print(datetime.datetime.utcnow()) 2015-02-18 04:53:31.783988 
>>> import pytz >>> then = datetime.datetime.now(pytz.utc) >>> then datetime.datetime(2015, 2, 18, 4, 55, 58, 753949, tzinfo=<UTC>) 
>>> datetime.datetime.now(datetime.timezone.utc) datetime.datetime(2015, 2, 18, 22, 31, 56, 564191, tzinfo=datetime.timezone.utc) 
>>> print(then) 2015-02-18 04:55:58.753949+00:00 >>> print(then.astimezone(pytz.timezone('US/Eastern'))) 2015-02-17 23:55:58.753949-05:00 
>>> pytz.utc.localize(datetime.datetime.utcnow()) datetime.datetime(2015, 2, 18, 6, 6, 29, 32285, tzinfo=<UTC>) >>> datetime.datetime.utcnow().replace(tzinfo=pytz.utc) datetime.datetime(2015, 2, 18, 6, 9, 30, 728550, tzinfo=<UTC>) 

Questions similaires