Advertisement

python学习笔记(5)time、datetime日期生成及格式转换

阅读量:

python日期处理主要有两个模块:time、datetime

time 模块

  1. 获取当前的时间戳 time.time()
  2. 将获取到的时间戳按照所需格式进行转换 time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(time_stamp_str))
  3. 获取实时的时间 time.localtime()
  4. 按照所需格式将实时的时间进行转换 time.strftime("%Y%m%d %H:%M:%S", time.localtime())
  5. 已知一个日期字符串 "2020-06-26 22:00:24" ,将其按照所需格式进行解析 time.strptime("2020-06-26 22:00:24", "%Y-%m-%d %H:%M:%S")
  6. 已知一个日期字符串 "..." ,将其按照所需方式转化为相应的时间值 time.mktime(...)
复制代码
    import time
    
    # 生成时间戳
    unix_time = time.time()
    print(unix_time)  # 1593179664.8451114
    print(type(unix_time))  # <class 'float'>
    print(int(unix_time))  # 1593179664
    
    # 已知时间戳,转换成固定格式的日期
    time_stamp = 1593179664
    time_array = time.localtime(time_stamp)  # 格式化时间戳为本地时间
    format_time = time.strftime("%Y/%m/%d %H:%M:%S",time_array)  # 2020/06/26 21:54:24
    
    # 生成时间数组,即:
    time_array = time.localtime()  # time.struct_time(tm_year=2020, tm_mon=6, tm_mday=26, tm_hour=22, tm_min=10, tm_sec=22, tm_wday=4, tm_yday=178, tm_isdst=0)
    print(time_array)
    print(time_array.tm_gmtoff)  # "offset from UTC in seconds
    print(time_array.tm_year)
    print(time_array.tm_mon)
    # 已知时间字符串和格式,转换成时间格式
    print(time.strptime("2020-06-26 22:00:24", "%Y-%m-%d %H:%M:%S"))
    # time.struct_time(tm_year=2020, tm_mon=6, tm_mday=26, tm_hour=22, tm_min=0, tm_sec=24, tm_wday=4, tm_yday=178, tm_isdst=-1)
    
    # 获取固定格式的当前时间
    format_time = time.strftime("%Y-%m-%d", time.localtime())  # 2020-06-26
    format_time = time.strftime("%Y%m%d %H:%M:%S", time.localtime())  # 20200626 21:59:56
    format_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())  # 2020-06-26 22:00:24
    # 已知时间字符串和格式,转换成其他时间格式
    format_time = time.strftime("%Y%m%d", time.strptime("2020-06-26 22:00:24", "%Y-%m-%d %H:%M:%S"))
    print(format_time)
    # 已知日期,转换成时间戳
    time_stamp = time.mktime(time.strptime("2020-06-26 22:00:24", "%Y-%m-%d %H:%M:%S"))  # 1593180024.0
    time_stamp = time.mktime(time.strptime("2020-06-26", "%Y-%m-%d"))  # 1593100800.0

datetime 模块

  1. 设置预设的具体时刻:datetime.datetime(2020, 6, 26, 8, 30, 30)
  2. 捕获当前时点:datetime.datetime.now()
  3. 捕获当天的日期记录:datetime.datetime.today()
  4. 解析对应的 datetime 对象:datetime.datetime.fromtimestamp(time_stamp_str)
复制代码
    import datetime
    
    # 用指定日期时间创建datetime
    dt = datetime.datetime(2020, 6, 26, 8, 30, 30)    # 2020-06-26 08:30:30
    # 把datetime转换为timestamp
    time_stamp = dt.timestamp()  # 1593131430.0
    # 获取当前时间
    now = datetime.datetime.now()  # 2020-06-26 22:27:36.202601
    print(type(now))  # <class 'datetime.datetime'>
    print(now)  # 2020-06-26 22:38:05.514118
    print(now.date())  # 2020-06-26
    print(now.day)  # 26
    # 获取当天日期
    print(datetime.datetime.today())
    # 格式转换,strftime需要为datetime.datetime格式日期才能直接调用
    format_time = now.strftime("%Y-%m-%d %H:%M:%S")  # 2020-06-26 22:59:22
    format_time = datetime.datetime.strftime(now, "%Y/%m/%d %H:%M:%S")  # 2020/06/26 22:59:22
    
    # 时间戳转换为时间
    time_stamp = 1593179664
    d = datetime.datetime.fromtimestamp(time_stamp)  # 2020-06-26 21:54:24
    d = datetime.datetime.fromtimestamp(time_stamp).strftime("%x")  # 06/26/20
    d = datetime.datetime.fromtimestamp(time_stamp).strftime("%c")  # Fri Jun 26 21:54:24 2020
    d = datetime.datetime.fromtimestamp(time_stamp).date()  # 2020-06-26

日期格式设置

该函数能够将时间类型值转换为指定的时间格式;
该函数可以将时间字符串解析为时间类型;
常用的格式如下:

复制代码
    '''
    %Y  Year with century as a decimal number.
    %m  Month as a decimal number [01,12].
    %d  Day of the month as a decimal number [01,31].
    %H  Hour (24-hour clock) as a decimal number [00,23].
    %M  Minute as a decimal number [00,59].
    %S  Second as a decimal number [00,61].
    %z  Time zone offset from UTC.
    %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.
    %I  Hour (12-hour clock) as a decimal number [01,12].
    %p  Locale's equivalent of either AM or PM.
    '''

全部评论 (0)

还没有任何评论哟~