• python时间库(time/datetime)


    1、time


    import time

    #1. 获得当前时间 -- 对应的时间元组
    tuple_time = time.localtime()
    print(tuple_time)

    #在时间元组中获得对应每一个想要获取的内容
    #1.通过脚标
    year = tuple_time[0]
    print(year)

    #2. 通过字段名来获取
    year = tuple_time.tm_year
    print(year)

    #4.获得当前时间对应的描述 [时间戳] 距离1970年1月1日凌晨的秒数
    current_second = time.time()
    print(current_second)

    #5. 涉及到知识点 时间格式化
    '''
    第一个参数放置的是时间格式化
      %Y
      %m
    第二个参数是要进行时间格式化的元组
    '''
    format_time = time.strftime("%Y/%m/%d %H:%M:%S %j %A", tuple_time)
    print(format_time)

    #将对应指定的格式化后的时间转化为时间元组
    format_time_1 = "2018-09-05 10:45:32"
    tuple_time_1 = time.strptime(format_time_1, "%Y-%m-%d %H:%M:%S")
    print(tuple_time_1)

    #6.获得自定义时间对应的秒数
    cus_second = time.mktime(tuple_time_1)
    print(cus_second)

    #7.将时间戳转换为时间元组
    cus_tuple = time.localtime(cus_second)
    print(cus_tuple)

    '''
    根据指定的格式化时间 获得其三天后对应的时间
    2018-05-02 17:22:33 ---> 2018-05-05 17:22:33
    '''
    def get_delay_daytime(format_timestr, days):
       #将格式化的时间转化为时间元组
       time_tuple = time.strptime(format_timestr, "%Y-%m-%d %H:%M:%S")
       #将时间元组转化为时间戳
       time_seconds = time.mktime(time_tuple)
       #在时间戳的基础上夹加上days对应的秒数
       end_seconds = time_seconds + days * 24 * 3600
       #在最后时间戳的基础上转化为时间元组
       end_time_tuple = time.localtime(end_seconds)
       #根据时间元组转化为最终的格式化时间
       end_format_time = time.strftime("%Y-%m-%d %H:%M:%S", end_time_tuple)
       return end_format_time

    res = get_delay_daytime("2018-05-02 17:22:33", 3)
    print(res)

    2、datetime


    import datetime

    #1.获取当前时间
    current_time = datetime.datetime.now()
    print(current_time)

    #2.获得当天的日期
    current_day = datetime.date.today()
    print(current_day)

    #3. 根据获得的日期 获得星期数
    week = current_day.weekday()
    print(week)


    #自定义时间
    #时分秒 不传的话 默认是凌晨
    cus_time = datetime.datetime(2018, 6, 27)
    print(cus_time)

    cus_time = datetime.datetime(2018, 6, 27, 10, 34, 51)
    print(cus_time)

    #获得自定义时间 两天前对应的时间
    get_time = cus_time + datetime.timedelta(days=3)
    print(get_time)
    print(type(cus_time))

    #时间格式化字符串类型
    format_time = get_time.strftime("%Y/%m/%d %p %I:%M:%S")
    print(format_time)

    #将格式化的时间转化为datetime的时间
    time_date = datetime.datetime.strptime(format_time, "%Y/%m/%d %p %I:%M:%S")
    print(time_date)

    #求两个时间的时间差
    time1 = datetime.datetime(2018, 7, 12, 12, 22, 18)
    time2 = datetime.datetime(2018, 8, 7, 10, 32, 19)

    defferent_time = time2 - time1
    print(defferent_time)

    #获得时间差中的天数
    day = defferent_time.days
    print(day)

    #获得时间差中的总秒数
    seconds = defferent_time.total_seconds()
    print(seconds)3、calendar

     

  • 相关阅读:
    java 传入多个参数时报"Parameter 'XXX' not found. Available parameters are [arg1, arg0, param1,..." 解决方案
    java 判断int类型为空
    scp 传输下载
    自己开发的网页在跳转至微信公众号文章后,点击微信的返回,无法返回原网页
    nginx下Thinkphp 隐藏index.php
    ubuntu常见错误–Could not get lock /var/lib/dpkg/lock解决
    apt-get update 和 upgrade 的区别
    php 取某一日期的前一天
    PHP 统计数组中所有的值出现的次数 array_count_values 函数
    pandas之表格样式
  • 原文地址:https://www.cnblogs.com/fansirs/p/13944556.html
Copyright © 2020-2023  润新知