artbarzz
artbarzz

Reputation: 93

Declaring a range of dates with datetime

I would like to know the best way to set a range of dates. I have logic that checks what day a certain record is approved on and based on that day i set a date the next time that record needs to be re-approved

so if the record is approved in january or february it should be re evaluated in march if its approved in march it should be re evaluated in june.

i declare my ranges like this for now, but this i believe is not the best way to do it

        DateTime quarterOneStart = new DateTime(DateTime.Now.Year,07,01);
        DateTime quarterOneEnd = new DateTime(DateTime.Now.Year, 09, 15));
        DateTime quarterTwoStart = new DateTime(DateTime.Now.Year, 10, 01);
        DateTime quarterTwoEnd = new DateTime(DateTime.Now.Year, 12, 15));
        DateTime quarterThreeStart = new DateTime(DateTime.Now.Year, 01, 01);
        DateTime quarterThreeEnd = new DateTime(DateTime.Now.Year, 03, 15));
        DateTime quarterFourStart = new DateTime(DateTime.Now.Year, 04, 01);
        DateTime quarterFourEnd = new DateTime(DateTime.Now.Year, 06, 15));

is there a better way to set the date time variables above?

Upvotes: 0

Views: 1742

Answers (3)

equin0x80
equin0x80

Reputation: 313

In the past I've written a FinancialYear class that internally holds a startYear (int) variable and offers various methods/properties, e.g. (among others)

public DateTime StartDate
{
    get { return new DateTime(_startYear, 4, 1); } // April 1st
}

public static FinancialYear ForDate(DateTime dt)
{
    DateTime finYearStart = new DateTime(dt.Year, 4, 1);
    return (dt >= finYearStart) ? new FinancialYear(dt.Year) : new FinancialYear(dt.Year - 1);
}

Upvotes: 0

praty
praty

Reputation: 573

Rather than creating multiple variables, you can create a Dictionary of items key'ed by an enum. For example:

public enum Quaters
{
    Q1_Start,
    Q1_End,
    Q2_Start,
    Q2_End,
    Q3_Start,
    Q3_End,
    Q4_Start,
    Q4_End
}

Dictionary<Quaters, DateTime> dateRange = new Dictionary<Quaters, DateTime>
{
    {Quaters.Q1_Start, new DateTime(DateTime.Now.Year, 07, 01)},
    {Quaters.Q1_End, new DateTime(DateTime.Now.Year, 09, 15)},
    {Quaters.Q2_Start, new DateTime(DateTime.Now.Year, 10, 01)},
    {Quaters.Q2_End, new DateTime(DateTime.Now.Year, 12, 15)},
    ...
};

When you need to verify the value against any range parameter you can index the dateRange dictionary based on keys. It is just a proposal and there could be better solutions to your actual problem.

Upvotes: 0

Roman Marusyk
Roman Marusyk

Reputation: 24619

What about class for quarter

public class Quarter {

    private readonly DateTime _startDate;
    private readonly DateTime _endDate;

    public Quarter(DateTime startDate, DateTime endDate) {
        _startDate = startDate;
        _endDate = endDate;
    }

    public DateTime StartDate => _startDate;
    public DateTime EndDate => _endDate;
}

and use it

Quarter one = new Quarter(new DateTime(2017, 07, 01), new DateTime(2017, 09, 15));
Quarter two = new Quarter(new DateTime(2017, 09, 15), new DateTime(2017, 10, 01));
...

Upvotes: 4

Related Questions