Python基础三--time与datetime模块

一、time与datetime模块:

  a、time模块:

  1. 时间戳(浮点型):time.time()--->1558758118.7347946; # 从1970年1月1日00:00:00开始按秒计算的偏移量;

  2. 格式化的时间字符串:time.strftime('%Y-%m-%d %X')--->2019-05-25 12:21:58;

%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.     
%d    Day of the month as a decimal number [01,31].     
%H    Hour (24-hour clock) as a decimal number [00,23].     
%I    Hour (12-hour clock) as a decimal number [01,12].     
%j    Day of the year as a decimal number [001,366].     
%m    Month as a decimal number [01,12].     
%M    Minute as a decimal number [00,59].     
%p    Locale’s equivalent of either AM or PM.    (1)
%S    Second as a decimal number [00,61].    (2)
%U    Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0.    (3)
%w    Weekday as a decimal number [0(Sunday),6].     
%W    Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0.    (3)
%x    Locale’s appropriate date representation.     
%X    Locale’s appropriate time representation.     
%y    Year without century as a decimal number [00,99].     
%Y    Year with century as a decimal number.     
%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].     
%Z    Time zone name (no characters if no time zone exists).     
%%    A literal '%' character.
  

  3. 结构化的时间(struct_time):struct_time元组共有9个元素共九个元素:(年,月,日,时,分,秒,一周中第几天,一年中第几天,夏令时)

time.localtime()--->time.struct_time(tm_year=2019, tm_mon=5, tm_mday=25, tm_hour=12, tm_min=21, tm_sec=58, tm_wday=5, tm_yday=145, tm_isdst=0);

  4. 将一个时间戳转换为UTC时区(0时区):time.gmtime()--->time.struct_time(tm_year=2019, tm_mon=5, tm_mday=25, tm_hour=4, tm_min=21, tm_sec=58, tm_wday=5, tm_yday=145, tm_isdst=0);

  

  5. 将一个结构化时间转化为时间戳:time.mktime(time.localtime())--->1558763371.0;

  6. 将一个结构化时间转化为格式化时间:time.strftime('%Y-%m-%d %X',time.localtime())--->2019-05-25 13:57:13;

  7. 将一个格式化时间字符串转化为结构化时间:time.strptime('2019-05-25 13:57:13','%Y-%m-%d %X')--->time.struct_time(tm_year=2019, tm_mon=5, tm_mday=25, tm_hour=13, tm_min=57, tm_sec=13, tm_wday=5, tm_yday=145, tm_isdst=-1);

    

  8. 将结构时间转换为 周几 几月 几号 时:分:秒 年 :time.asctime(time.localtime())--->Sat May 25 15:41:13 2019;

  9. 将时间戳转换为 周几 几月 几号 时:分:秒 年 :time.ctime(time.time())--->Sat May 25 15:41:13 2019;

  b. datetime模块 (时间加减)

  1. 当前时间:datetime.datetime.now()--->2019-05-25 16:00:11.794955;

  2.  当前日期:datetime.date.fromtimestamp(time.time())--->2019-05-25;

  3.  当前时间+2天:datetime.datetime.now()+datetime.timedelta(2)--->2019-05-27 16:00:11.794955;

  4.  当前时间+1小时:datetime.datetime.now()+datetime.timedelta(hours=1)--->2019-05-25 17:00:11.794955;

  5. 当前时间:datetime.datetime.now()+datetime.timedelta(minutes=1)--->2019-05-25 16:01:11.794955;

  6. 替换时间:datetime.datetime.now().replace(minute=12,hour=12)--->2019-05-25 12:12:11.794955;

原文地址:https://www.cnblogs.com/gangzi4321/p/10922043.html