Naphstor
Naphstor

Reputation: 2496

Calculate datetime difference in C#

Hi I was solving a problem to calculate some library fine based on difference in return date and due date in C#. Now there are some constraints to the problem like

if the return year is changed i.e. if the return year is greater than the due date calendar year then fine is 10000. e.g. due date "31/12/2015" and return date "01/01/2016" then also fine is 10000.

if the return month is changed then fine is 500 * number of months late.

if the return day is changed then fine is 15 * number of days late.

else fine is 0.

Now i wrote the function below:

static int CalcFine (int[] returnedOn, int[] dueOn) {
    int returnD = returnedOn[0];
    int returnM = returnedOn[1];
    int returnY = returnedOn[2];
    int dueD = dueOn[0];
    int dueM = dueOn[1];
    int dueY = dueOn[2];

    if (returnY > dueY) {
        return 10000;
    } else if (returnY < dueY) {
        return 0;
    } else {
        if (returnM > dueM) {
            return (returnM - dueM) * 500;
        } else if (returnM < dueM) {
            return 0;
        } else {
            if (returnD > dueD) {
                return (returnD - dueD) * 15;
            } else {
                return 0;
            }
        }
    }

}

I read about the DateTime class in C# that has pretty neat functions that return the difference in two dates as total days, total minutes, etc. But given the constraint of Fine being different based on year, month and days, I am not sure if there is any other inbuilt function to solve the above problem. In short I am trying to find if there is another simple way to solve the above problem without using so many if-else's.

Upvotes: 1

Views: 848

Answers (3)

gayan1991
gayan1991

Reputation: 795

You can get the difference in days, hours or minutes.

DateTime fromdate = new DateTime(2012,1,1);
DateTime todate = DateTime.Now;

TimeSpan diff = todate - fromdate;
int differenceInDays = diff.Days;

If you want to try differently for your validations and business rules. Follow the below code

public double GetFineAmount(DateTime DueDate)
{
    DateTime dt = DateTime.Now;
    int yeardiff, monthdiff, daydiff;

    yeardiff = dt.Year - DueDate.Year;
    if (yeardiff > 0) return 10000;
    monthdiff = dt.Month - DueDate.Month;
    if (monthdiff > 0) return 500 * monthdiff;
    daydiff = dt.Day - DueDate.Day;
    if (daydiff > 0) return 15 * daydiff;
    return 0;
}

Upvotes: 2

Jan Paolo Go
Jan Paolo Go

Reputation: 6512

Editted again.. changed string pattern. I guess I need some sleep...

    static int CalcFine (string returnedOn, string dueOn)
    {
        DateTime returnedDate = DateTime.ParseExact(
            returnedOn, "d M yyyy", CultureInfo.InvariantCulture);
        DateTime dueDate = DateTime.ParseExact(
            dueOn, "d M yyyy", CultureInfo.InvariantCulture);

        if (returnedDate < dueDate)
            return 0;
        if (returnedDate.Year > dueDate.Year)
            return 10000;
        if (returnedDate.Month > dueDate.Month)
            return 500 * (returnedDate.Month - dueDate.Month);
        if (returnedDate.Day > dueDate.Day)
            return 15 * (returnedDate.Day - dueDate.Day);
        else
            return 0;
    }

Upvotes: 1

Kramb
Kramb

Reputation: 1092

DateTime is a powerful tool. But you don't want to over-complicate this.

If you just find the difference between the two dates in days, the equation becomes a lot easier to manage versus trying to subtract dates.

static int CalcFine(DateTime returnedOn, DateTime dueOn)
    {

        TimeSpan dateDiff = (returnedOn - dueOn);
        int TotalDays = dateDiff.Days;
        if (TotalDays >= 365)
        {
            return 10000;
        }
        else if(TotalDays < 365 && TotalDays > 30 && TotalDays % 30 > 1)
        {
            return (500 * (TotalDays % 30));
        }
        else if(TotalDays < 30 && TotalDays > 0)
        {
            return 15 * TotalDays;
        }
        else
        {
            return 0;
        }
    }

Upvotes: 0

Related Questions