python中datetime模块的使用

datetime模块

  • 导入: import datetime

  • 使用

    • date

      • # 获取当前日期
        datetime.date.today()  # 2022-11-14
        
        # 设置指定日期
        datetime.date(year=2023, month=12, day=2)  # 2023-12-02
        datetime.date(2023, 12, 2)  # 位置传参
        datetime.date.today().year  # 2022
        datetime.date.today().month  # 11
        datetime.date.today().day  # 14
        
        # 格式化日期
        datetime.date.ctime(datetime.date.today())  # Mon Nov 14 00:00:00 2022
        datetime.date.strftime(datetime.date.today(), "%Y-%m-%d %H:%M:%S")  # 2022-11-14 00:00:00
        datetime.date.isoformat(datetime.date.today())  # 2022-11-14, iso格式化时间(年-月-日)
        datetime.date.isoweekday(datetime.date.today())  # 1 iso格式化时间,返回星期几
        datetime.date.isocalendar(datetime.date.today())  # (2022, 46, 1) iso格式化时间(年,周数,星期几)
        datetime.date.toordinal(datetime.date.today())  # 738473,计算公元公历到现在的天数
        datetime.date.fromordinal(738473)  # 2022-11-14,将公元公历到现在的天数格式化
        
        # 更改日期
        datetime.date.today().replace(year=2023, day=1)  # 2023-11-01
        
        # 根据时间戳计算时间
        datetime.date.fromtimestamp(1668433821.824537)  # 2022-11-14
        
        # 返回一个时间元组
        datetime.date.timetuple(datetime.date.today())  # time.struct_time(tm_year=2022, tm_mon=11, tm_mday=14, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=318, tm_isdst=-1)
        
        # 获取日期的最大值
        datetime.date.max  # 9999-12-31
        
        # 获取日期的最小值
        datetime.date.min  # 0001-01-01
        
        
    • datetime

      • # datetime.datetime对象中的属性
        datetime_obj = datetime.datetime.today()
        datetime_obj.year  # 年
        datetime_obj.month  # 月
        datetime_obj.hour  # 时
        
        # 获取当前datetime.datetime对象
        datetime.datetime.today()  # 2022-11-16 21:57:04.868518
        datetime.datetime.now()  # 2022-11-16 21:57:04.868518,()中可以指定时区
        
        # 根据时间戳获取对应datetime.datetime对象
        datetime.datetime.fromtimestamp(time.time())  # atetime.datetime.fromtimestamp(时间戳[时区])
        
        # 将时间字符串转为datetime.datetime对象
        datetime.datetime.strptime("2022-11-16 22:03:58", "%Y-%m-%d %H:%M:%S")  # strptime("时间字符串", "格式")
        
        # 将datetime.datetime对象转换为时间字符串
        datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")  # 2022-11-16 22:20:17
        
        # 将datetime.datetime类的对象转换为datetime.date对象
        a = datetime.datetime.time(datetime.datetime.now())  # 2022-11-16
        
        # 将datetime.datetime类的对象转换为datetime.time对象
        b = datetime.datetime.time(datetime.datetime.now())  # 22:08:44.874375
        
        # 将datetime.date对象和datetime.time对象转换成datetime.datetime对象
        datetime.datetime.combine(a, b)  # 2022-11-16 22:11:07.424325
        
        # 修改datetime.datetime对象中的属性
        datetime.datetime.now().replace(year=2023, month=1)  # 2023-01-16 22:13:48.148241
        
        # 获取datetime.datetime的结构化时间
        datetime.datetime.now().timetuple()  # time.struct_time(tm_year=2022, tm_mon=11, tm_mday=16, tm_hour=22, tm_min=15, tm_sec=33, tm_wday=2, tm_yday=320, tm_isdst=-1)
        
        # 获取今天是星期几(0代表星期一,这里2代表星期三)
        datetime.datetime.weekday(datetime.datetime.now())  # 2
        
        # 返回一个元组(年,周数,星期几)
        datetime.datetime.isocalendar(datetime.datetime.now())  # (2022, 46, 3)
        
        
    • timedelta

      • # timedelta是用来表示两个时间的时间差的
        now = datetime.datetime.now()  # now = 2022-11-16 22:27:54.520353
        
        # 获取到一天后的datetime.datetime对象
        n_time = datetime.timedelta(days=1)
        next_day = now + d_time  # next_day = 2022-11-17 22:27:54.520353
        
        # 获取到一天前的datetime.datetime对象
        p_time = datetime.timedelta(days=-1)
        pre_day = now + p_time  # pre_day = 2022-11-15 22:27:54.520353,也可以now-n_time得到一天前的
        
        # 获取到1天2小时4分钟30秒后的datetime.datetime对象
        a = now + datetime.timedelta(days=1, hours=2, minutes=4, seconds=30)  # 2022-11-18 00:39:57.286777
        
        # 获取1天2小时4分钟30秒是多少秒
        datetime.timedelta(days=1, hours=2, minutes=4, seconds=30).total_seconds()  # 93870.0
        
        
    • time

      • # datetime.time类的实例化
        a = datetime.time(1, 2, 3, 4)  # 01:02:03.000004  时(hour):分(minute):秒(second):微秒(microsecond) 时区(tzinfo)
        
        # replace修改
        b = a.replace(hour=4, minute=5)  # b = 04:05:03.000004
        
        # 格式化datetime.time对象
        a.strftime("%H:%M:%S")  # 01:02:03
        a.__format__("%H:%M:%S")  # 01:02:03
        a.isoformat()  # 国际格式化 01:02:03.000004
        
        

你可能感兴趣的:(python模块,python,开发语言)