Python time&datetime模块

1.time&datetime模块

  time&datetime是时间模块,常用以处理时间相关问题

time.time()        #返回当前时间的时间戳timestamp

time.sleep()       #睡眠时间,默认秒

time.gmtime()    #时间戳转换成UTC时间的struct_time

time.localtime()  #时间戳转换成返回本地时间的struct_time

time.asctime()    #struct_time转换成本地时间的标准化string时间

time.ctime()       #时间戳转换成本地时间的标准化string时间

time.mktime()    #struct_time转换成本地时间的时间戳timestamp

time.strftime()    #解析struct_time,自定义格式化显示时间

time.strptime()    #解析格式化时间,返回struct_time

 

  常用功能使用说明

# -*- coding:utf-8 -*-
# Author:Wong Du

'''
    time&datetime是时间模块,
    常用以处理时间相关问题
'''


import time
'''
time模块时间的3种形式
1.timestamp 时间戳
    e.g. 1521164794.9068174
2.struct_time 结构化时间(元组形式表示)
    e.g. time.struct_time(tm_year=2078, tm_mon=6, tm_mday=25, tm_hour=22, 
         tm_min=57, tm_sec=22, tm_wday=5, tm_yday=176, tm_isdst=0)
3.string_time 字符串表示(时间的人性化显示,一般人类可读)
    e.g. Fri Mar 16 09:49:13 2018
'''
# 时间戳
print(time.time())
# 结构化
print(time.localtime())
# 字符串
print(time.asctime())

# time.asctime(p_tuple=None) --> string,
# 解析一个tuple结构化时间,返回其标准化时间的string,如:Fri Mar 16 09:51:26 2018,
# 若tuple为空,则返回当前时间的string
m = (2018, 3, 16, 10, 0, 52, 4, 75, 0)
print(time.asctime(m))

# time.ctime(seconds=None) --> string,
# 时间戳转化成标准化时间的string,若seconds为空,
# 则返回当前时间的string
print(time.ctime(2222222222))

# time.gmtime(seconds=None) --> struct_time,
# 时间戳转换成UTC时间的struct_time,和中国时间差8个小时
# 若seconds为空,则返回当前UTC时间的struct_time
print(time.gmtime(2222222))

# time.localtime(seconds=None) --> struct_time,
# 时间戳转换成struct_time,
# 若seconds为空,则返回当前的struct_time
print(time.localtime(2222222))

# time.mktime(p_tuple) --> timestamp
# 把struct_time转换成时间戳timestamp,
print(time.mktime(m))


# 获取本地当前时间的时间戳
print(time.time())

# 设置睡眠时间,单位秒
time.sleep(0.1)

# time.strftime(format,p_tuple=None) --> format_time
# 解析p_tuple,自定义格式显示,如:2018-01-03,
# 若p_tuple为空,则格式化显示本地当前时间
print(time.strftime("%Y-%m-%d %H:%M:%S",m))

# time.strptime(string,format) --> struct_time,
# 解析string,获取到string表示的struct_time
print(time.strptime("2018-1-1","%Y-%m-%d"))


import datetime
# 获取本地当前时间,格式: %Y-%m-%d %H:%M:%S
print(datetime.datetime.now())

# 时间加减,默认顺序:days,秒,微秒,毫秒,minutes,hours,weeks
print(datetime.datetime.now() + datetime.timedelta(1))  #+1day
print(datetime.datetime.now() + datetime.timedelta(-3))  #-3day
print(datetime.datetime.now() + datetime.timedelta(minutes=-10))  #-10minutes
print(datetime.datetime.now() + datetime.timedelta(weeks=1))  #+7day

# 时间替换
"""
replace(self, year=None, month=None, day=None, hour=None,
                minute=None, second=None, microsecond=None, tzinfo=True)
"""
print(datetime.datetime.now().replace(year=2022))
常用功能详解

 

  其他

时间格式化显示:

%Y  Year with century as a decimal number.
%m  Month as a decimal number [01,12].
%d  Day of the month as a decimal number [01,31].
%H  Hour (24-hour clock) as a decimal number [00,23].
%M  Minute as a decimal number [00,59].
%S  Second as a decimal number [00,61].
%z  Time zone offset from UTC.
%a  Locale's abbreviated weekday name.
%A  Locale's full weekday name.
%b  Locale's abbreviated month name.
%B  Locale's full month name.
%c  Locale's appropriate date and time representation.
%I  Hour (12-hour clock) as a decimal number [01,12].
%p  Locale's equivalent of either AM or PM.

 

  三种时间格式关系示例图

 

posted @ 2018-03-16 13:53  糕事情  阅读(221)  评论(0编辑  收藏  举报