P
P
pcdesign2015-07-17 18:06:41
css
pcdesign, 2015-07-17 18:06:41

How to check if a date is within the range of current quarter or previous quarter +25 days in python?

How to check if a date is within the range of current quarter or previous quarter +25 days in python?
For example, let the date format be
YYYY-MM-DD, then it turns out:
2015-07-10 is included (current quarter)
2015-03-07 is not included
2015-06-25 is included (last quarter + 25 days)
2015-04-21 included (last quarter +25 days)
2014-06-30 not included
2015-01-02 not included
Etc.
Today we have 2015-07-17 and the condition "last quarter + 25 days is still working." But 2015-07-26 already the entire last quarter should be discarded as non-compliant.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
shmatuan, 2018-11-29
@z_u_q

If flex, then so)
It would be more convenient through grid
https://codepen.io/anon/pen/MzzdrV
UPD issued a response through grids (code below) https://codepen.io/anon/pen/Jeeqmq

C
coderxx, 2018-11-29
@coderxx

You can use flex
https://codepen.io/anon/pen/XyywJZ

P
pcdesign, 2015-07-19
@pcdesign

Threw something like that.
It seems to work.

from datetime_periods import period
from dateutil.parser import parse
import math
import datetime

current_date = datetime.datetime.now()


def get_num_quarter(ref):  # Возвращает номер квартала
    return math.ceil(ref.month/3.)


def start_end_quarter(ref):  # Возвращает первый и последний день квартала
    return period(ref, 'quarter')


def previous_quarter(ref):  # Возвращает 1-ый день предыдущего квартала
    quarter = (ref.month - 1) // 3
    prev_quarter = (quarter - 1) % 4
    return datetime.datetime(ref.year if quarter > 0
                             else ref.year-1, prev_quarter*3+1, 1)

dates = "2015-07-10 2015-03-07 2015-06-25 2015-04-21 2014-07-30".split(" ")

for d in dates:
    date_obj = parse(d)
    # Проверяем входит ли дата в текущий квартал
    if (current_date.year == date_obj.year
            and get_num_quarter(date_obj) == get_num_quarter(current_date)):
        print(d + " - Входит. Т.к. дата относится к текущему кварталу")
        continue

    # Проверяем условие: предыдущий кв. + 25 дней
    prev_date = previous_quarter(current_date)
    start = start_end_quarter(prev_date)[0]
    end = start_end_quarter(prev_date)[1] + datetime.timedelta(days=25)
    if start < date_obj and current_date < end:
        print(d + " - Входит. Т.к. предыдущий кв. + 25 дней еще не закончились")
    else:
        print(d + " - Не входит в диапазон")

Result:
2015-07-10 - Входит. Т.к. дата относится к текущему кварталу
2015-03-07 - Не входит в диапазон
2015-06-25 - Входит. Т.к. предыдущий кв. + 25 дней еще не закончились
2015-04-21 - Входит. Т.к. предыдущий кв. + 25 дней еще не закончились
2014-07-30 - Не входит в диапазон

R
Roman Kitaev, 2015-07-17
@deliro

datetime and timedelta.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question