Jump to content

[Python]Marcajul temporal Python la datetime si invers


Recommended Posts

Exemplul 1: Marcajul temporal Python la datetime:

from datetime import datetime

timestamp = 1545730073
dt_object = datetime.fromtimestamp(timestamp)

print("dt_object =", dt_object)
print("type(dt_object) =", type(dt_object))

Cand executati programul, rezultatul va fi:

dt_object = 2018-12-25 09:27:53
type(dt_object) = <class 'datetime.datetime'>

Aici, am importat clasa datetime din modulul datetime. Apoi, am folosit datetime.fromtimestamp() classmethod care returneaza data si ora locala (obiectul datetime). Acest obiect este stocat in dt_object .


Exemplul 2: Python datetime to timestamp:

from datetime import datetime

# current date and time
now = datetime.now()

timestamp = datetime.timestamp(now)
print("timestamp =", timestamp)


Link to comment
Share on other sites

  • Tupi locked this topic
This topic is now closed to further replies.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.