MadBoy
MadBoy

Reputation: 11104

How to find out how many quarters there are beween one quarter and the other in c#

I've created a class called Kwartal (which translates into Quarter) for use in my program:

    public sealed class Kwartal {
        private DateTime _poczatekKwartalu;
        private DateTime _koniecKwartalu;
        private int _numerKwartalu;
        private int _rok;

        public Kwartal(int numer, DateTime dataod, DateTime datado) {
            _numerKwartalu = numer;
            _koniecKwartalu = datado;
            _poczatekKwartalu = dataod;
            _rok = dataod.Year;
        }
        public Kwartal() { }

        public int Numer {
            get { return _numerKwartalu; }
            set { _numerKwartalu = value; }
        }
        public DateTime DataPoczatkowa {
            get { return _poczatekKwartalu; }
            set { _poczatekKwartalu = value; }
        }
        public DateTime DataKoncowa {
            get { return _koniecKwartalu; }
            set { _koniecKwartalu = value; }
        }
        public int Rok {
            get { return _rok; }
            set { _rok = value; }
        }
    }

It's basically definition for Quarter. Usually i define it like this:

Kwartal kwartal1 = new Kwartal(1, new DateTime(year, 1, 1), new DateTime(year, 3, 31));
Kwartal kwartal2 = new Kwartal(2, new DateTime(year, 4, 1), new DateTime(year, 6, 30));

Now i was wondering how I can do math on those. For example I've got Quarter1 in 2011 and i then have Quarter3 in 2012. I would like to find out how many quarters are there between Quarter1 and Quarter3.

Like kwartal2 - kwartal1 = 5

Upvotes: 1

Views: 2295

Answers (7)

user687474
user687474

Reputation:

You can use the DateDiff class of the Time Period Library for .NET, respecting the calendar culture:

// ----------------------------------------------------------------------
public void DateDiffSample()
{
  DateTime date1 = new DateTime( 2009, 11, 8, 7, 13, 59 );
  Console.WriteLine( "Date1: {0}", date1 );
  // > Date1: 08.11.2009 07:13:59
  DateTime date2 = new DateTime( 2011, 3, 20, 19, 55, 28 );
  Console.WriteLine( "Date2: {0}", date2 );
  // > Date2: 20.03.2011 19:55:28

  DateDiff dateDiff = new DateDiff( date1, date2 );
  Console.WriteLine( "DateDiff.Quarters: {0}", dateDiff.Quarters );
  // > DateDiff.Quarters: 5
} // DateDiffSample

Upvotes: 1

Tomas Jansson
Tomas Jansson

Reputation: 23462

From your answer, this is what I would do:

public static int zwrocRozniceMiedzyKwartalami(Kwartal kwartal1, Kwartal kwartal2) {
    var quartersPerYear = 4;
    var yearDifference = kwartal2.Rok - kwartal1.Rok;
    var differenceQuarters = (yearDifference * quartersPerYear) + (kwartal2.Numer - kwartal1.Numer);
    return differenceQuarters;
}

I think this would give you the following answers:

(Year1, Quarter1) - (Year2, Quarter2) = Difference

(2012, 2) - (2011, 1) = (2011 - 2012)*4 + (1 - 2) = -4 + (-1) = -5 => (2011, 1) is 5 quarters before (2012, 2)

(2014, 1) - (2018,3) = (2018 - 2014)*4 + (3 - 1) = 16 + 2 = 18 => (2018,3) is 18 months after (2014,1)

Upvotes: 1

MadBoy
MadBoy

Reputation: 11104

This is how i ended up doing this. Seems to be working fine. If you've got a better way lemme know :-)

    public static int zwrocRozniceMiedzyKwartalami(Kwartal kwartal1, Kwartal kwartal2) {
        int quartersPerYear = 4;
        int differenceQuarters;
        int yearDifference = kwartal2.Rok - kwartal1.Rok;
        if (yearDifference == 0) {
            differenceQuarters = kwartal2.Numer - kwartal1.Numer;
            return differenceQuarters;
        } else if (yearDifference > 0) {
            differenceQuarters = (yearDifference * quartersPerYear) +  (kwartal2.Numer - kwartal1.Numer);
            return differenceQuarters;
        } else if (yearDifference < 0) {
            return -1;
        }
        return -1;  
    }

Upvotes: 0

Blazes
Blazes

Reputation: 4779

If you convert the year / quarters to decimal, and do a little math, you can calculate the difference.

class DateTimeQuarter
{
    public DateTimeQuarter(DateTime date)
    {
        Date = date;
        Quarter = date.Month / 4 + 1;
    }
    public static int operator -(DateTimeQuarter lhs, DateTimeQuarter rhs)
    {
        double value = Convert.ToDouble(
            (rhs.Date.Year - lhs.Date.Year)) + (rhs.Quarter / 10.0) - (rhs.Quarter / 10.0);
        int result = 
            (Convert.ToInt32(value) * 4) + Convert.ToInt32(value - Math.Floor(value));
        return result;
    }
    public DateTime Date { get; set; }
    public int Quarter { get; set; }
}

static void Main(string[] args)
{
    DateTimeQuarter q1 = new DateTimeQuarter(new DateTime(2006, 04, 20));
    DateTimeQuarter q2 = new DateTimeQuarter(new DateTime(2007, 12, 25));

    int quarters = q1 - q2;
}

Upvotes: 0

V4Vendetta
V4Vendetta

Reputation: 38200

I am not sure whether this is what you want to achieve

DateTime quarter1 = new DateTime(2010, 3, 31); //end date of Q1 as you capture DataKoncowa 
DateTime quarter2 = new DateTime(2011, 3, 31);//end date of Q2 as you capture DataKoncowa 
TimeSpan ts = quarter2 - quarter1; //kwartal2.DataKoncowa  - kwartal1.DataKoncowa
int actualQuarters = ts.Days / (30 *3); //assuming quarter is 90 days

This returns 4 .. something like what you expect

Upvotes: 0

GvS
GvS

Reputation: 52518

First, you should define what the result of the calculation should be. Create a matrix/table with some data, and a lot of edge cases, and calculate by hand what the result should be.

Then, you could figure out an algorithm to use to calculate it. And basicly implement this algorithm. And test on your data you made up.

Oh, and then you start thinking about time-zones, daylight saving, etc. And you should read this blog entry from Jon Skeet.

Upvotes: 0

Unsliced
Unsliced

Reputation: 10552

Nowhere do you define the number of quarters in the year - without there being some property/constant set on the Kwartal object how can you know what the base value should be for your subtraction operation?

Once you've set that base value, the operation would be reasonably easy, you can create an absolute quarter count, e.g.

k.QuarterCount = (k1.year * kwartal.base) + k1.quarter

then you have an integer to substract from the other year.

Upvotes: 2

Related Questions