zoukankan      html  css  js  c++  java
  • python minus 3 days or n days

    python内置的方法,太简单了。。。
    from datetime import datetime,timedelta

    d = datetime.now()
    z = d + timedelta(days=-7)
    print str(z).split(' ')[0].split('-')[1]

    import calendar

    DAY_QUERY = 4

    def day_end_month(year, month):
    cal = calendar.month(year, month)
    return cal[-3:-1]

    def modify_time(time_str):
    # kwargs = {'earliest_time':'2017-02-20T00:00:00.000',
    # 'latest_time':'',
    # 'search_mode':'normal',
    # 'exec_mode':'blocking'}

    kwargs = {'earliest_time': '', 
              'latest_time': '', 
              'search_mode': 'normal', 
              'exec_mode': 'blocking'}
    
    append_hms = 'T00:00:00.000'
    
    # time_str: 2017-02-20
    tmp_year = time_str.split('-')[0]
    tmp_month = time_str.split('-')[1]
    tmp_day = time_str.split('-')[2]
    
    tmp_month_int = int(tmp_month)
    tmp_day_int = int(tmp_day)
    
    if tmp_day_int > 9+DAY_QUERY:
        # just modify month
        day_gt_13 = tmp_year + '-' + tmp_month + '-' + str(tmp_day_int - DAY_QUERY)
    
        kwargs['earliest_time'] = day_gt_13 + append_hms
        kwargs['latest_time'] = time_str + append_hms
    
    # 4-12
    if tmp_day_int > DAY_QUERY and tmp_day_int < 10+DAY_QUERY and tmp_month_int > 9:
        # modify day with add 0(zero)
        day_gt_3_and_lt_13 = tmp_year + '-' + tmp_month + '-' + '0' + str(tmp_day_int - DAY_QUERY)
    
        kwargs['earliest_time'] = day_gt_3_and_lt_13 + append_hms
        kwargs['latest_time'] = time_str + append_hms
    
    if tmp_day_int > DAY_QUERY and tmp_day_int < 10+DAY_QUERY and tmp_month_int > 1 and tmp_month_int < 10:
        x_day_gt_3_and_lt_13 = tmp_year + '-' + tmp_month + '-' + '0' + str(tmp_day_int - DAY_QUERY)
    
        kwargs['earliest_time'] = x_day_gt_3_and_lt_13 + append_hms
        kwargs['latest_time'] = time_str + append_hms
    
    
    # 1-3 and month > 1
    if tmp_day_int < 1+DAY_QUERY and tmp_month_int > 1:
        # modify day and month
        # put year and month with int
        tmp_new_day_gt_1 = int(day_end_month(int(tmp_year), tmp_month_int-1))
    
        # str(tmp_new_day_gt_1 - (3 - tmp_day_int)): get end of day of last month
        month_day_lt_4_and_month_gt_1 = tmp_year + '-' + '0' + str(tmp_month_int - 1) + '-' + str(
            tmp_new_day_gt_1 - (DAY_QUERY - tmp_day_int))
    
        kwargs['earliest_time'] = month_day_lt_4_and_month_gt_1 + append_hms
        kwargs['latest_time'] = time_str + append_hms
    
    
    if tmp_day_int < 1+DAY_QUERY and tmp_month_int == 1:
        pass
        # modify year, month and day
        tmp_new_month_eq_1 = '12'
        tmp_new_year_eq_1 = str(int(tmp_year) - 1)
    
        tmp_new_day_eq_1 = int(day_end_month(int(tmp_year), tmp_month_int))
    
        year_lt_4_and_month_eq_1 = tmp_new_year_eq_1 + '-' + tmp_new_month_eq_1 + '-' + str(
            tmp_new_day_eq_1 - (DAY_QUERY - tmp_day_int))
    
        kwargs['earliest_time'] = year_lt_4_and_month_eq_1 + append_hms
        kwargs['latest_time'] = time_str + append_hms
    
    if tmp_day_int > DAY_QUERY and tmp_day_int < 10+DAY_QUERY and tmp_month_int == 1:
        l_day_gt_3_and_lt_13 = tmp_year + '-' + tmp_month + '-' + '0' + str(tmp_day_int - DAY_QUERY)
    
        kwargs['earliest_time'] = l_day_gt_3_and_lt_13 + append_hms
        kwargs['latest_time'] = time_str + append_hms
    
    if tmp_day_int > 9+DAY_QUERY and tmp_month_int == 1:
        n_day_gt_13 = tmp_year + '-' + tmp_month + '-' + str(tmp_day_int - DAY_QUERY)
    
        kwargs['earliest_time'] = n_day_gt_13 + append_hms
        kwargs['latest_time'] = time_str + append_hms
    
    return kwargs
    

    print modify_time('2017-01-12')

  • 相关阅读:
    this.$route和this.$router的区别
    IE不支持 Promise 解决办法
    滚动定位的多种方法
    css设置禁止文字被选中
    input标签内容改变的触发事件
    webpack-dev-server配置指南webpack3.0
    Object.keys方法之详解
    mac安装webpack失败
    vue2使用animate css
    http
  • 原文地址:https://www.cnblogs.com/otfsenter/p/6420764.html
Copyright © 2011-2022 走看看