Home > OS >  How to convert time() to datetime.now()
How to convert time() to datetime.now()

Time:03-25

from datetime import datetime
from time import time

print(f'{datetime.now()} and {time()}')

Yield 2022-03-25 16:27:37.051854 and 1648193257.051854
I hope to getdatetime.now() from time().

I found Sec: 37 = 1648193257 % 60.
27469887 = 1648193257 // 60
min: 27 = 27469887 % 60

But I could not find how can yield hour data and so on.

CodePudding user response:

I recommend that you review this documentation

As an example, you can get the current hour as follows:

import time

print(time.localtime().tm_hour)

CodePudding user response:

You can use the datetime.datetime.fromtimestamp() function for it.

print(datetime.datetime.now())
now = time.time()
print(now)
print(datetime.datetime.fromtimestamp(now))

Yields

2022-03-25 09:13:42.665469
1648196022.6654692
2022-03-25 09:13:42.665469

A similar question with answers you can find here

  • Related