Python内置库:time和datetime(时间和日期)

time模块

  time模块通常用来操作时间戳信息(各种“秒”),常用的方法有:

  • time.sleep(seconds):将当前程序阻塞指定秒数,然后继续运行程序。
  • time.time():返回当前时间的时间戳,即1970年到现在经过的浮点秒数。
  • time.struct_time:struct_time类初始化时需要传入一个由9个时间信息组成的元组,该元组的时间信息依次为:tm_year=2018(年),tm_mon=8(月),tm_mday=8(日),tm_hour=23(时),tm_min=38(分),tm_sec=54(秒),tm_wday=2(星期,星期一为0),tm_yday=220(一年中的第几天),tm_isdst=0(1表示支持夏令时,0表示不支持夏令时,-1表示未知)。当然,一个对象是struct_time对象,可以通过属性的方式获取这些值,比如:time.localtime().tm_year。
  • time.localtime(seconds=None):返回当前时间的struct_time对象,也可以指定秒数seconds(默认采用time.time()的int值),则返回1970加上指定秒数后的struct_time对象。
  • time.mktime(p_tuple):将struct_time对象(元组)转换为时间戳,然后返回。
  • time.strptime(string, format):将时间字符串string按照指定格式format转化成struct_time对象,如:time.strptime('2016-09-04', '%Y-%m-%d')
  • time.strftime(format, p_tuple=None):将struct_time对象(元组)转换成指定格式的字符串,默认使用time.localtime()的结果来进行转换,如:time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())。

  注:方法中的参数p_tuple表示可以是struct_time对象,也可以是满足struct_time对象初始化的时间信息元组,其实struct_time对象就可以看成是一个由时间信息组成的元组

>>> import time
>>> # time.time()方法,返回当前时间的时间戳(1970年到现在经过的浮点秒数)
>>> time.time()
1473691580.9504104
>>> # time.struct_time对象,存储时间信息
>>> structtime_obj = time.struct_time((2018, 8, 8, 23, 38, 54, 2, 220, 0))
>>> structtime_obj
time.struct_time(tm_year=2018, tm_mon=8, tm_mday=8, tm_hour=23, tm_min=38, tm_sec=54, tm_wday=2, tm_yday=220, tm_isdst=0)
>>> structtime_obj.tm_year
2018
>>> # time.localtime()方法,默认返回即当前时间(time.time()的int值)的struct_time对象
>>> time.localtime()
time.struct_time(tm_year=2018, tm_mon=8, tm_mday=8, tm_hour=23, tm_min=22, tm_sec=2, tm_wday=2, tm_yday=220, tm_isdst=0)
>>> time.localtime(time.time())
time.struct_time(tm_year=2018, tm_mon=8, tm_mday=8, tm_hour=23, tm_min=22, tm_sec=2, tm_wday=2, tm_yday=220, tm_isdst=0)
>>> time.localtime(80000)
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=2, tm_hour=6, tm_min=13, tm_sec=20, tm_wday=4, tm_yday=2, tm_isdst=0)
>>> # time.mktime()方法,将struct_time对象转换成时间戳
>>> time.mktime(time.localtime())
1533746402.0
>>> # time.strptime()方法,将字符串按照指定格式转换为struct_time对象
>>> time.strptime('2016-09-04', '%Y-%m-%d')
time.struct_time(tm_year=2016, tm_mon=9, tm_mday=4, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=248, tm_isdst=-1)
>>> # time.strftime()方法,将struct_time对象转换成指定格式的字符串
>>> time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
'2018-08-09 00:43:04'

 datetime模块  

  datetime模块通常用来操作日期信息(年月日和时分秒),常用的方法有:

  • datetime.datetime.now():返回当前日期时间的datetime对象,对象中包含年月日和时分秒信息,可通过str它来得到日期时间信息的字符串。
  • datetime.datetime.fromtimestamp(timestamp):将时间戳转换成datetime对象,并返回。
  • datetime.datetime.strptime(date_string, format):将字符串按照指定格式转成datetime对象,并返回,如:datetime.datetime.strptime('2016-09-04', '%Y-%m-%d')。
  • datetime.datetime.strftime(datetime, format):将datetime对象转换为指定格式的字符串,并返回,如:datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S')。
  • datetime.timedelta:timedelta对象初始化时指定日期时间信息,可用它与datetime对象进行加减操作,并返回新的datetime对象,当然timedelta对象之间也可以进行加减操作,返回新的timedelta对象。
  • timetuple():将datetime对象转换成struct_time对象,并返回。
>>> import datetime
>>> # now()方法,返回当期时间的datetime对象,可通过str得到日期时间信息的字符串
>>> datetime_now = datetime.datetime.now()
>>> datetime_now
datetime.datetime(2018, 8, 9, 0, 59, 3, 487000)
>>> str(datetime_now)
'2018-08-09 00:59:03.487000'
>>> # fromtimestamp()方法,将时间戳转换成datetime对象,并返回
>>> datetime.datetime.fromtimestamp(time.time())
datetime.datetime(2018, 8, 9, 1, 2, 51, 546000)
>>> # strptime()方法,将字符串按照指定格式转成datetime对象
>>> datetime.datetime.strptime('2016-09-04', '%Y-%m-%d')
datetime.datetime(2016, 9, 4, 0, 0)
>>> # strftime()方法,将datetime对象转换为指定格式的字符串,并返回
>>> datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S')
'2018-08-09 01:18:10'
>>> # timedelta对象,可用于日期时间信息的加减操作
>>> timedelta_hour = datetime.timedelta(hours=4)
>>> timedelta_day = datetime.timedelta(days=8)
>>> timedelta_new = timedelta_hour + timedelta_day
>>> timedelta_hour
datetime.timedelta(0, 14400)
>>> timedelta_day
datetime.timedelta(8)
>>> timedelta_new
datetime.timedelta(8, 14400)
>>> datetime.datetime.now()
datetime.datetime(2018, 8, 9, 1, 30, 0, 593000)
>>> datetime_new = datetime.datetime.now() + timedelta_new
>>> datetime_new
datetime.datetime(2018, 8, 17, 5, 30, 1, 70000)
>>> # timetuple()方法,将datetime对象转换成struct_time对象
>>> now_datetime = datetime.datetime.now()
>>> now_datetime.timetuple()
time.struct_time(tm_year=2018, tm_mon=8, tm_mday=9, tm_hour=2, tm_min=5, tm_sec=50, tm_wday=3, tm_yday=221, tm_isdst=-1)
日期时间格式化字符串
%a         星期的缩写。如星期三Wed
%A 星期的全名。如星期三Wednesday
%b 月份的缩写。如四月份Apr
%B 月份的全名。如4月份April
%c 日期和时间字符串表示。如04/07/10 10:43:39
%d 每月的第几天
%p AM或PM
%H 小时(24小时格式)
%I 小时(12小时格式)
%j 一年中的第几天
%m 月份
%M 分钟
%S
%f 微秒
%U 一年中的第几周(星期天为一周的第一天,新的一年的第一个星期天被认为是第0周的开始)
%w 星期几(星期天为0)
%W 一年中的第几周(星期一为一周的第一天,新的一年的第一个星期一被认为是第0周的开始)
%x 日期字符串
%X 时间字符串
%y 年份(不含世纪,即两个数字表示)
%Y 年份(含世纪,即4个数字表示)
%Z 时区名称
%z Time zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal hour digits and M represents decimal minute digits [-23:59, +23:59].
%%

百分号“%”

原文地址:https://www.cnblogs.com/guyuyun/p/5839893.html