56-Common methods of time-related modules

56-Common methods of time-related modules

import time

t = time.localtime() # Return the nine-tuple of the current time
time.gmtime() # Returns the nine-tuple of the current time in Greenwich 0 time zone
time.time() # Commonly used, the number of seconds between 1970-1-1 8:00, timestamp
time.mktime(t) # Convert the nine-tuple time into a timestamp
time.sleep(1)
time.asctime() # If there are parameters, it is in the form of a nine-tuple
time.ctime() # Returns the current time, the parameter is a timestamp, commonly used
time.strftime("%Y-%m-%d") # Commonly used
time.strptime('2018-07-20', "%Y-%m-%d") # returns the nine-tuple time format
time.strftime('%H:%M:%S')

########################################
from datetime import datetime
from datetime import timedelta
datetime.today() # Return the datetime object of the current time
datetime.now() # Same as above, time zone can be used as parameter
datetime.strptime('2018/06/30','%Y/%m/%d') # return datetime object
dt = datetime.today()
datetime.ctime(dt)
datetime.strftime(dt, "%Y%m%d")

days = timedelta(days=90, hours=3) # commonly used
dt2 = dt + days
dt2.year
dt2.month
dt2.day
dt2.hour
Reference: https://cloud.tencent.com/developer/article/1334425 56-Common methods of time-related modules-Cloud + Community-Tencent Cloud