Python学习 —— 常用库(calendar、time、datetime)

版权声明:转载注明出处 https://blog.csdn.net/qq_19428987/article/details/86515344

calendar 日历模块

  • calendar(year, w=2, l=1, c=6, m=3):以多行字符串的形式返回一年的日历。w=每个日期之间的间隔字符数;l=每周所占的行数;c=每个月之间的间隔字符数。

    import calendar
    cal=calendar.calendar(2019)
    print(cal)
    
  • isleap(year):判断指定是否为闰年,是为True,平年为False

  • isleapdays(year1,year2):获取指定年份之间闰年数量

    print(calendar.isleap(2018))
    
  • month(year,month):获取某个月的日历字符串

    print(calendar.month(2019,3))
    >>>     March 2019
    Mo Tu We Th Fr Sa Su
             	1  2  3
    4  5  6  7  8  9 10
    11 12 13 14 15 16 17
    18 19 20 21 22 23 24
    25 26 27 28 29 30 31
    
  • weekday(year,month,day):获取指定日期为星期几,注意周一到周天分别对应0~6

    print(calendar.weekday(2019,3,1))
    >>> 4
    

    time 模块

    时间戳: 一个时间表示,根据语言的不用,可以是整数或者浮点数。表示从1970年1月1日0时0分0秒到现在经历的秒数(目前32位操作系统可以支持到2038年)

  • 时间元组:一个包含时间内容的普通元组(可读不可写)

序号 属性
0 tm_year 年份
1 tm_mon 月份(1~12)
2 tm_mday 日期(1~31)
3 tm_hour 小时(0~23)
4 tm_min 分(0~59)
5 tm_sec
6 tm_wday 0~6(0是周一)
7 tm_yday 1~366(一年的第几天)
8 tm_isdst -1, 0, 1, -1是决定是否为夏令时的旗帜
  • time.time() : 获取当前时间戳

  • time.loacltime() 获取当前时间结构,返回当前时间元组:

    import time
    t=time.localtime()
    print(t)
    >>>time.struct_time(tm_year=2019, tm_mon=1, tm_mday=16, tm_hour=22, tm_min=6, tm_sec=35, tm_wday=2, tm_yday=16, tm_isdst=0)
    
  • asctime():返回元组的正常字符串话之后的形式的时间格式

    showTime=time.asctime(t)
    print(showTime)
    >>>Wed Jan 16 22:09:34 2019
    
  • sleep(n):是程序进入睡眠,n秒后继续

  • strftime:将时间元组转化成自定义的字符串格式:
    1、 %Y 四位数的年份表示(000-9999)
    2、%m 月份(01-12)
    3、%d 月内中的一天(0-31)
    4、%H 24小时制小时数(0-23)
    5、%M 分钟数(00=59)
    6、%S 秒(00-59)
    python中时间日期格式化符号

    import time
    t=time.localtime()
    ft=time.strftime("%Y年%m月%d日%H:%M",t)
    print(ft)
    

datetime 模块

datatime模块重新封装了time模块,提供更多接口,提供的类有:date,time,datetime,timedelta,tzinfo

  • datetime.date(year, month, day):提供日期信息

  • date.today() 函数:返回一个当前本地日期的date类型的对象

    from datetime import *
    day=datetime.today()
    print(day.date())
    >>>2019-01-17
    
  • datetime.fromtimestamp(时间戳):根据给定的时间戳返回一个时间date

    from datetime import *
    import time
    now=time.time()
    day=datetime.fromtimestamp(now)
    print(day.date())
    >>>2019-01-17 20:48:21.122437
    
  • date.weekday(day)函数:返回指定的日期是周几(周一对应0)

    from datetime import *
    import time
    day=datetime.today()
    s=datetime.weekday(day)
    print(s)
    >>> 3
    
  • date.strftime(fmt): 返回自定义格式的时间字符串。fmt是自定义的时间格式

    from datetime import *
    import time
    day=datetime.today()
    show=day.date().strftime("%Y-%m-%d")
    print(show)
    >>>2019-01-17
    
  • replace(year, month, day):生成一个新的日期对象 用参数指定的年,月,日代替原有对象中的属性。

    from datetime import *
    import time
    day=datetime.today()
    day=day.replace(2018,3,24)
    print(day)
    >>>2018-03-24 20:59:16.588928
    
  • 日期的其他操作–日期加上一个时间间隔(datetime.timedela类的对象:使用timedelta可以很方便的在日期上做天days,小时hour,分钟,秒,毫秒,微妙的时间计算)

    from datetime import *
    import time
    day=datetime.today()
    b=timedelta(hours=1)
    day=day+b
    print(day)
    >>>2019-01-17 21:42:26.642116
    

猜你喜欢

转载自blog.csdn.net/qq_19428987/article/details/86515344