比较2个时刻日期字串的时间差

比较2个时刻日期字串的时间差

一、背景
有时需要比较2个时刻日期字串的时间差
'2009-02-28 00:00:04'
'2010-03-01 01:02:03'
时间差为
1年0个月6天 1个小时1分钟59秒

二、分析
采用python,
编写比较函数:gap_start2end('2009-02-28 00:00:04','2010-03-01 01:02:03')
返回时间差的组合表示数:((1, 0, 6), (1, 1, 59))

三、封装好的函数方法

import time
import datetime
import math


# 一、相同日期格式的时间距离

def date2obj(date_str='2015-04-07 19:11:21', format='%Y-%m-%d %H:%M:%S'):
    '''
    日期字串 转 日期对象
    :param date_str:日期字串
    :param format:日期字串格式
    :return:日期对象
    d_obj.year  输出 2015
    d_obj.month  输出 4
    d_obj.day  输出 7
    '''
    t_str = date_str
    d_obj = datetime.datetime.strptime(t_str, format)

    return d_obj


# 1、s秒时间间隔
def time_seconds_span(start_date1, end_date2, fomart1='%Y-%m-%d %H:%M:%S', fomart2='%Y-%m-%d %H:%M:%S'):
    d1_obj = date2obj(start_date1, fomart1)
    d2_obj = date2obj(end_date2, fomart2)

    return (d2_obj - d1_obj).seconds


# 2、min分钟时间间隔
def time_minutes_span(start_date1, end_date2, fomart1='%Y-%m-%d %H:%M:%S', fomart2='%Y-%m-%d %H:%M:%S'):
    d1_obj = date2obj(start_date1, fomart1)
    d2_obj = date2obj(end_date2, fomart2)
    seconds_span = (d2_obj - d1_obj).seconds
    # 向下取整
    mins_span = math.floor(seconds_span / 60)

    return mins_span


# 3、h小时时间间隔
def time_hours_span(start_date1, end_date2, fomart1='%Y-%m-%d %H:%M:%S', fomart2='%Y-%m-%d %H:%M:%S'):
    d1_obj = date2obj(start_date1, fomart1)
    d2_obj = date2obj(end_date2, fomart2)
    seconds_span = (d2_obj - d1_obj).seconds
    # 向下取整
    hours_span = math.floor(seconds_span / (60 * 60))

    return hours_span


# 4、d天数时间间隔。无论是否带有h、min、s,计算时,会忽略h、min、s,只计算天数、月数和年数。
def date_days_span(start_date1, end_date2, fomart1='%Y-%m-%d', fomart2='%Y-%m-%d'):
    d1_obj = date2obj(start_date1, fomart1)
    d2_obj = date2obj(end_date2, fomart2)

    # 只取天数差值。等同于向下取整。
    days_span = (d2_obj - d1_obj).days

    return days_span


# 5、month月数时间间隔。无论是否带有h、min、s,计算时,会忽略h、min、s,只计算天数、月数和年数。
def date_months_span(start_date1, end_date2, fomart1='%Y-%m-%d', fomart2='%Y-%m-%d'):
    d1_obj = date2obj(start_date1, fomart1)
    d2_obj = date2obj(end_date2, fomart2)

    # 向下取整。只取天数。
    days_span = (d2_obj - d1_obj).days
    months_span = math.floor(days_span / 30)

    return months_span


# 6、year年数时间间隔。无论是否带有h、min、s,计算时,会忽略h、min、s,只计算天数、月数和年数。
def date_years_span(start_date1, end_date2, fomart1='%Y-%m-%d', fomart2='%Y-%m-%d'):
    d1_obj = date2obj(start_date1, fomart1)
    d2_obj = date2obj(end_date2, fomart2)

    # 向下取整。只取天数。
    days_span = (d2_obj - d1_obj).days
    years_span = math.floor(days_span / (12 * 30))

    return years_span


def gap_start2end(start_str, end_str, start_fomart='%Y-%m-%d %H:%M:%S', end_fomart='%Y-%m-%d %H:%M:%S'):
    '''
    比较两个时刻的时间差
    :param start_str: 起始时刻日期字串
    :param end_str: 截止时刻日期字串
    :param start_fomart: 起始时刻日期字串读取格式
    :param end_fomart: 截止时刻日期字串读取格式
    :return: 时刻差值组合数的数组((y, m, d), (h, min, sec))
    # 举例说明:
    '2009-02-28 00:00:04' ,
    '2010-03-01 01:02:03'
    输出((1, 0, 6), (1, 1, 59))
    含义为:时间间隔1年0个月6天,1小时1分钟59秒

    # 时间回推说明:
    # sum_days = y * 30 * 12 + m * 30 + d
    # sum_seconds = sum_days * 24 * 60 * 60 + h * 60 * 60 + min * 60 + sec
    '''

    # 年数差
    years_gap = date_years_span(start_str, end_str, start_fomart, end_fomart)
    # 月数差
    months_gap = date_months_span(start_str, end_str, start_fomart, end_fomart)
    # 天数差
    days_gap = date_days_span(start_str, end_str, start_fomart, end_fomart)
    # 取出日期的组合表示数
    y = years_gap
    m = months_gap % 12
    d = days_gap % 30

    # 小时数差
    hours_gap = time_hours_span(start_str, end_str, start_fomart, end_fomart)
    # 分钟数差
    mins_gap = time_minutes_span(start_str, end_str, start_fomart, end_fomart)
    # 秒数差
    seconds_gap = time_seconds_span(start_str, end_str, start_fomart, end_fomart)
    # 取出时间的组合表示数
    h = hours_gap
    min = mins_gap % 60
    sec = seconds_gap % 60

    return ((y, m, d), (h, min, sec))


if __name__ == '__main__':
    # # 1、计算秒差。
    # start_date1 = '2015-04-07 00:00:00'
    # end_date1 = '2015-04-08 00:01:01'
    # seconds_span = time_seconds_span(start_date1, end_date1)
    # print(seconds_span)
    #
    # # 2、计算分钟差。向下取整
    # start_date2 = '2015-04-07 00:00:00'
    # end_date2 = '2015-04-07 00:01:01'
    # mins_span = time_minutes_span(start_date2, end_date2)
    # print(mins_span)
    #
    # # 3、计算小时差。向下取整
    # start_date3 = '2015-04-07 00:00:00'
    # end_date3 = '2015-04-07 02:01:00'
    # hours_span = time_hours_span(start_date3, end_date3)
    # print(hours_span)
    #
    # # 4、计算天数差。向下取整
    # start_date4 = '2015-04-07'
    # end_date4 = '2015-04-15'
    # days_span = date_days_span(start_date4, end_date4)
    # print(days_span)
    #
    # # 5、计算月数差。向下取整
    # start_date5 = '2015-04-02'
    # end_date5 = '2015-05-03'
    # months_span = date_months_span(start_date5, end_date5)
    # print(months_span)
    #
    # # 6、计算年数差。向下取整
    # start_date6 = '2015-04-02'
    # end_date6 = '2017-05-03'
    # years_span = date_years_span(start_date6, end_date6)
    # print(years_span)

    # 一、相同日期格式的时间距离
    start_date = '2009-02-28 00:00:04'
    end_date = '2010-03-01 01:02:03'
    cha = gap_start2end(start_date, end_date)
    print(cha)

  

posted @ 2020-04-03 17:18  安迪9468  阅读(250)  评论(0编辑  收藏  举报