Python datetime module

time module

Timestamp: Timestamp represents the offset in seconds from January 1, 1970 00:00:00, that is, the total number of milliseconds from January 01, 1970 00:00:00 to the present

time module

The time() function is used to return the timestamp of the current time

Example
import time
print("The current timestamp is {0}".format(time.time()))

The current timestamp of the execution result
is 1524406087.3988266


The localtime() function
formats timestamps as local time

import time
print("time.localtime {0}".format(time.localtime()))

结果:
time.localtime time.struct_time(tm_year=2018, tm_mon=4, tm_mday=22, tm_hour=22, tm_min=19, tm_sec=32, tm_wday=6, tm_yday=112, tm_isdst=0)


asctime() function

Returns a readable form for receiving time tuples

import time
t = time.localtime()
print("time.asctime {0}".format(time.asctime(t)))

Results
time.asctime Sun Apr 22 22:24:28 2018


The sleep() function is
used to postpone the running of the calling thread

Format
time.sleep(secs)


import time

print("Start {0}".format(time.ctime()))
time.sleep(10)
print("End {0}".format(time.ctime()))


Result:
Start Sun Apr 22 22:26:54 2018
End Sun Apr 22 22:27:04 2018

 

datetime module

Classes defined by the datetime module

datetime.date: Class representing date, common attributes year, month, day
datetime.time Class representing time, common attributes hour, minute, second, micsecond
datetime.datetime: Representing date and time
datetime.timedelta: Representing time interval, that is, two length of time point

today() current local time
import datetime

print("today is: {0}".format(datetime.datetime.today()))


now()

import datetime

print("today is: {0}".format(datetime.datetime.now()))

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324671210&siteId=291194637