Datetime, c # datetime
# Import datetime
# String to datetime:
# Notice:
# When the String type is converted to the datetime type, the date format must match
a = '2016-03-23' b = datetime.datetime.strptime(a,'%Y-%m-%d')
# Datetime to String:
# Notice:
# When datetime is converted to the String type, it can be converted to any format
now = datetime.datetime.now()
# Obtain the current time:
now = datetime.datetime.now()
# Get today's date:
today = datetime.date.today()
# Obtain the date of yesterday:
yesterday = today - datetime.timedelta(days=1)
# Obtain n days, n hours, before and after a date:
# Example: Get the date of the previous 8 days
now = datetime.datetime.now() date_before_8 = now - datetime.timedelta(days=8)
# Example: Get the date 12 days after the present:
date_after_12 = now + datetime.timedelta(days=12)
# Example: 8 hours before the current time:
date_before_8_hours = now - datetime.timedelta(hours=8)
# Obtain the date and time objects of datetime respectively:
now = datetime.datetime.now(). now.date() now.time()
# Obtain the year, month, and day of datetime respectively:
now.year now.month now.day
# Obtain the time difference between two datetime values, and return the number of days:
(date1 - date2).days