Blog

Home   /   blog   /   Cara Mendapatkan Waktu Terkini di Python

Cara Mendapatkan Waktu Terkini di Python

Mungkin kita ingin waktu saat ini dalam bentuk tanggal raw atau timestamp. Dapat juga berupa tanggal namun mengikuti format standard.

Di Python Ada beberapa cara untuk mendapatkan waktu saat ini.

In [1]: import time

In [2]: import datetime

In [3]: time.time()
Out[3]: 1484267692.901511

In [4]: time.ctime()
Out[4: 'Fri Jan 13 07:34:56 2017'

In [5]: datetime.datetime.now()
Out[5]: datetime.datetime(2017, 1, 13, 7, 35, 0, 848495)

In [6]: x = datetime.datetime.now()

In [7]: x
Out[7]: datetime.datetime(2017, 1, 13, 7, 35, 12, 913230)

In [8]: print x
2017-01-13 07:35:12.913230

In [9]: x.second
Out[9]: 12

In [10]: x.year
Out[10]: 2017

In [11]: x.month
Out[11]: 1

In [12]: x.day
Out[12]: 13

In [13]: x.hour
Out[13]: 7

In [14]: x.minute
Out[14]: 35

In [15]: x.microsecond
Out[15]: 913230

(rfs/python)

Leave a Reply

Your email address will not be published.