-time basis and python datetime module

datetime time module and module

time module

time module time performance formats are mainly three kinds:

  • timestamp Timestamp : representation is from the beginning of January 1970 00:00:00 01 offset press in seconds. That is, from January 1, 1970 00:00:00 from the current time to the total time in seconds
  • Format String formatting time : the time the formatted structure more readable. Comprising a fixed format and custom format . It may be used to splice the user and save time format used

  • struct_time time formatting objects : time tuples, the tuple elements in 9 groups , representing: year, month, day, hour, minute, second, day of the week (0-6), the first few year days, whether daylight saving time (-1)

FIG time format conversion

FIG time format conversion

Note: Formatting time into a timestamp, and then converted to format the time, gmtime () and localtime () results may differ by one day

The reason: gmtime moved out of Greenwich Mean Time, localtime is taken into account the time zone

import time

# time() 获取当前时间戳
timestamp1 = time.time()
print(f"当前时间戳:{timestamp1}")  # 输出结果:1573889052.7453291

# 获取当前年月日(yyyy-MM-dd)
str_day = time.strftime("%Y-%m-%d")
print(f"格式化后的年月日:{str_day}")  # 输出结果:2019-11-16
# 获取当前年月日时分秒(yyyy-MM-dd hh:mm:ss)
# %X 和 %H:%M:%S 效果是一样的
str_time = time.strftime("%Y-%m-%d %H:%M:%S")
str_time1 = time.strftime("%Y-%m-%d %X")  # 建议使用这种
print(f"[%H:%M:%S]格式化后的年月日时分秒:{str_time}")
# 输出结果:2019-11-16 15:24:12
print(f"[%X]格式后的年月日时分秒:{str_time1}")
# 输出结果:2019-11-16 15:24:12

# localtime() 获取当前时区时间对象,东八区(北京时间)
time_obj = time.localtime()
print(f"localtime获取的时间对象:{time_obj}")
# gmtime() 获取格林威治时间对象
time_obj2 = time.gmtime()
print(f"gmtime获取的时间对象:{time_obj2}")

# 时间戳转换成时间对象 localtime/gmtime
struct_stamp = time.localtime(time.time())
struct_stamp2 = time.gmtime(time.time())
print(f"localtime:{struct_stamp}")
print(f"gmtime:{struct_stamp2}")

# mktime() 将时间对象转换成时间戳
stamp_mk = time.mktime(time.localtime())
print(f"当前时间对象:{stamp_mk}")

# 格式化时间转换成时间对象 strptime
struct_for = time.strptime("2019-10-22", "%Y-%m-%d")
print(f"strptime:{struct_for}")

# 时间戳转换成格式化时间 ctime
res = time.ctime(1573888219)
print(res)  # 固定的时间格式:Sat Nov 16 15:10:19 2019

# 时间对象转换成格式化时间 asctime
# 2019-09-18 17:23:34 后续3个数字随意输入的
tup_time = (2019, 9, 18, 17, 23, 34, 1, 3, 14)
res_time = time.asctime(tup_time)
print(res_time)  # 固定的时间格式:Tue Sep 18 17:23:34 2019
  • strftime format code
format meaning
%Y Year, for example: 2019
%m In May, the format of XX, [01-12]
%d Day, in the format XX, [01,31].
%H , The 24-hour, [00,23].
%M Min, [00,59].
%S Sec [00,61]. Is 60 leap seconds, 61 reserved for historical reasons

datetime module

import datetime

# 获取当前年月日
print(datetime.date.today())

# 获取当前年月日时分秒
print(datetime.datetime.today())

# datetime.datetime.today() 获取的是一个时间对象
time_obj = datetime.datetime.today()
print(f"年:{time_obj.year}")
print(f"月:{time_obj.month}")
print(f"日:{time_obj.day}")

'''
日期/时间的计算
    日期时间 = 日期时间 “+” or “-” 时间对象
    时间对象 = 日期时间 “+” or “-” 日期时间
'''
# 当前时间
current_time = datetime.datetime.now()
# timedelta:日期时间间隔对象
time_object = datetime.timedelta(days=7)
# 获取7天之后的时间
later_time = current_time + time_object
print(later_time)

# 日期/时间 之间计算,返回时间对象
timestamp = current_time - later_time
print(timestamp)

Guess you like

Origin www.cnblogs.com/xiaodan1040/p/11872271.html