美文网首页
py3笔记4:datetime常见时间处理方法

py3笔记4:datetime常见时间处理方法

作者: _百草_ | 来源:发表于2021-11-22 11:06 被阅读0次
    #-*-coding:utf-8-*-
    """
    @author:百草
    @file:getEmail_note.py
    @time:2021/09/06
    """
    import datetime
    # 1. 获取当前时间
    now = datetime.datetime.now()
    tod = datetime.datetime.today()
    print(f"today= {tod}")  # today= 2021-09-07 22:26:30.494616
    print(f"now = {now}")  # now = 2021-09-07 22:26:30.494616
    
    now_time =now.time()
    print(now_time)  # 22:49:07.616614
    print(now.minute)  # 49 分
    print(now.hour)    # 小时,22
    print(now.second)  # 整数秒,52
    
    # 返回一个time结构
    print("time结构:",now.timetuple(), type(now.timetuple()))
    # time.struct_time(tm_year=2021, tm_mon=9, tm_mday=7, tm_hour=22, tm_min=31,
    #                   tm_sec=32, tm_wday=1, tm_yday=250, tm_isdst=-1) <class 'time.struct_time'>
    
    # 返回一个date结构
    print("date结构:", now.date(), type(now.date()))  # date结构: 2021-09-07 <class 'datetime.date'>
    
    # 返回一个time类型
    print("time类型:", now.time())  # time类型: 22:33:46.180543
    
    # 返回当前星期几
    print("今天星期几(0-周一;6-周日):", now.weekday())  # 今天星期几(0-周一;6-周日): 1
    # 0-周一;6-周日
    
    # 返回当前星期几
    print("今天星期几(1-周一;7-周日):", now.isoweekday())  # 今天星期几(1-周一;7-周日): 2
    
    # ========================================================
    
    # 2. 当前时间添加/减少
    time2 = now - datetime.timedelta(hours=1)
    #     timedelta(days: float = ..., seconds: float = ..., ,
    #               microseconds: float,  # 微秒
    #               milliseconds: float,  # 毫秒
    #               minutes: float = ..., hours: float = ...,
    #               weeks: float = ...)
    print(time2)  # 2021-09-06 20:49:52.668491
    
    # 3. 格式化字符串输出
    time_string = now.strftime("%Y/%m/%d was a %a")
    print(time_string)  # 2021/09/07 was a Tuesday
    # %Y 年,如2021(4位);   %y 年,如21,后两位
    # %m ,月,如09;  # %M  分,如49
    # %d, 日,如07;   %D 日期,如09/07/21 月/日/年(2位)
    # %H 时,如22(24小时制度);%h 月份的英文缩写,如Sep
    # %S 秒;  %s 无效
    # %A, 星期,如Tuesday; %a ,如Tue
    
    # 4. 字符串转化为时间类型
    time3 = "2021/09/07"
    time3 = datetime.datetime.strptime(time3,"%Y/%m/%d")
    # 第1个参数time_string:字符串类型的时间;
    # 第2个参数format:time_string的格式
    print(time3, type(time3))  # 2021-09-07 00:00:00  <class 'datetime.datetime'>
    
    # 5. 修改当前时间
    time4 = now.replace(year=2020)
    print("修改后的时间=", time4)  # 修改后的时间= 2020-09-07 22:38:13.150296
    # 参数:year=None, month=None, day=None, hour=None, minute=None, second=None, microsecond=None, tzinfo=True, fold=None
    
    # 6.进行比较运算,返回timedelta类型
    past = datetime.datetime(2021, 8, 7, 20, 11, 59)
    time5 = now-past
    print("比较运算结果:", time5, type(time5))  # 比较运算结果: 31 days, 2:30:59.896544 <class 'datetime.timedelta'>
    
    # ========================================================
    # 练习:本月的第1天,本周的第一天
    def get_first_day_of_month():
        day = datetime.datetime.now().day  # 今天的日day
        return datetime.datetime.today()-datetime.timedelta(days=day-1)
    
    print(f"获取本月第一天:{get_first_day_of_month()}")
    
    def get_first_day_of_week():
        day = datetime.datetime.now().weekday()  # 周几
        print(day)  # Tueday,阿拉伯数字=1
        return datetime.datetime.now()-datetime.timedelta(days=day)
    
    print(f"获取本周第一天:{get_first_day_of_week()}")
    

    相关文章

      网友评论

          本文标题:py3笔记4:datetime常见时间处理方法

          本文链接:https://www.haomeiwen.com/subject/eekmwltx.html