Click here to Skip to main content
15,896,727 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to calculate the difference between two dates, but all months must be 30 days.

So February 28th & 29th doesn't matter.
All months 30 day.

I calculate lıke that for example:

12.04.2010
12.04.2007 3 year 0 month 0 day

Of course these dates can be different you enter with datetimepicker.
Posted
Updated 7-Jul-10 3:43am
v2
Comments
Richard MacCutchan 7-Jul-10 9:32am    
And what exactly is your problem?
Dalek Dave 7-Jul-10 9:44am    
WHY?

What is wrong with having the correct answer?
William Winner 7-Jul-10 14:01pm    
so what do you do when you get a date like 12.31.2010? since all months have 30 days in your program, that date isn't possible!
Kythen 7-Jul-10 15:05pm    
If you explain *why* you have these requirements and give us the bigger picture of what you are trying to accomplish you are more likely to get a better answer. I'm guessing this has something to do with either pay periods or fiscal calendars, but obviously there is no way for me to know for sure.

Not sure if this is what you want (don't quite get the 'all months must be 30 days'):

DateTime now = DateTime.Now;
DateTime yesterday = DateTime.Now.AddDays(-1);

int diff = now.Subtract(yesterday).Days;
Console.WriteLine(diff);
 
Share this answer
 
Comments
Alain Bertrand 7-Jul-10 10:56am    
Reason for my vote of 5
that's how I would have solved it too
C#
private void CalculateDifference()
{
    int years, months, days;

    years = (dateTimePicker2.Value.Year - dateTimePicker1.Value.Year);
    months = (dateTimePicker2.Value.Month - dateTimePicker1.Value.Month);
    days = (dateTimePicker2.Value.Day - dateTimePicker1.Value.Day);
    
    if (months < 0)
    {
        years--;
        months = months + 12;
    }

    if (days < 0)
    {
        months--;
        days = days + 30;
    }

    textBox1.Text = years + " Years, " + months + " Months, " + days + " Days";
}


This gives the following results for the following inputs

First DateSecond DateDate Difference
10.07.200907.07.20100 Years, 9 Months, 0 Days
10.07.200910.08.20101 Years, 0 Months, 1 Days
12.16.200910.08.20100 Years, 9 Months, 22 Days
06.08.200907.07.20100 Years, 0 Months, 29 Days
02.07.201003.04.20100 Years, 0 Months, 27 Days*


*The last example is actually inaccurate, though, because February does not have 30 days. In actuality, the difference between March 4th and February 7th is 25 days on a non-leap year and 26 on a leap year, not 27. But that's what you asked for.
 
Share this answer
 
v3
Comments
Member-2338430 8-Jul-10 2:05am    
Reason for my vote of 5
THANKS
Basically you have/want to create a custom calendar for an year that has 12 months each having 30 days.

Since the requirement is not a normal date manipulations where Feb can have 28 or 29 and few other months 31... you have to create your own DateClass that can handle such operations.

In the class, define normal date manipulations you want to have.
Your logic would be based on 12*30 = 360 days in an year. (30 days equally distributed across the year for each month)

Further, this custom class would help you in your datepicker too. As currently, none of the datepicker just shows 30 days for each month.
Strange requirement, you need to create something new for yourself.
 
Share this answer
 
Comments
jasonHall 7-Jul-10 10:14am    
Reason for my vote of 4
A custom DateClass seems to be the only solution as an alternative date/time continuum is being abstracted.
This is a very strange requirement and some explanation of why it's required could lead to a better answer.

Anyway, taking your question as you have posed it, you will need a custom struct. System.DateTime is basically a wrapper around a long that represents the number of ticks since 1st Jan 0001. You need to do something similar. As you only need day and not tick precision I would suggest wrapping an int. once that is done the rest is fairly standard stuff.

I have coded (not tested) a basic sample [because I found the problem sort of interesting] that meets your requirements except how the span is displayed - you can call GetDaySpan and use some math on the result to get what you want. There is a lot of optimisation that could be done but I leave that up to you too.

Enjoy...
C#
public struct WierdDate
{
    public const int DaysPerMonth = 30;
    public const int DaysPerYear = DaysPerMonth * MonthsPerYear;
    public const int MaxYear = 9999;
    public const int MonthsPerYear = 12;
    public static readonly WierdDate MaxWierdDate = new WierdDate(DaysPerMonth, MonthsPerYear, MaxYear);
    public static readonly WierdDate MinWierdDate = new WierdDate();
    private int days;
    private WierdDate(int days)
    {
        this.days = days;
    }
    public WierdDate(int day, int month, int year)
    {
        if (day < 1 || day > DaysPerMonth)
            throw new ArgumentOutOfRangeException("day", string.Format("day must be between 1 and {0}", DaysPerMonth));
        if (month < 1 || month > MonthsPerYear)
            throw new ArgumentOutOfRangeException("month", string.Format("month must be between 1 and {0}", MonthsPerYear));
        if (year < 1 || year > MaxYear)
            throw new ArgumentOutOfRangeException("year", string.Format("year must be between 1 and {0}", MaxYear));
        days = ((year - 1) * DaysPerYear) + ((month - 1) * DaysPerMonth) + (day - 1);
    }
    public static bool operator ==(WierdDate wierdDateA, WierdDate wierdDateB)
    {
        return wierdDateA.days == wierdDateB.days;
    }
    public static bool operator !=(WierdDate wierdDateA, WierdDate wierdDateB)
    {
        return !(wierdDateA == wierdDateB);
    }
    public static WierdDate operator ++(WierdDate wierdDate)
    {
        return wierdDate + 1;
    }
    public static WierdDate operator --(WierdDate wierdDate)
    {
        return wierdDate - 1;
    }
    public static WierdDate operator +(WierdDate wierdDate, int value)
    {
        return new WierdDate(wierdDate.days + value);
    }
    public static WierdDate operator -(WierdDate wierdDate, int value)
    {
        return new WierdDate(wierdDate.days - value);
    }
    public static bool operator >(WierdDate wierdDateA, WierdDate wierdDateB)
    {
        return wierdDateA.days > wierdDateB.days;
    }
    public static bool operator <(WierdDate wierdDateA, WierdDate wierdDateB)
    {
        return wierdDateA.days < wierdDateB.days;
    }
    public static bool operator >=(WierdDate wierdDateA, WierdDate wierdDateB)
    {
        return wierdDateA.days >= wierdDateB.days;
    }
    public static bool operator <=(WierdDate wierdDateA, WierdDate wierdDateB)
    {
        return wierdDateA.days <= wierdDateB.days;
    }
    public int Day
    {
        get { return (TotalDays - (TotalMonths * DaysPerMonth)) + 1; }
    }
    public int Month
    {
        get { return (TotalMonths - (TotalYears * MonthsPerYear)) + 1; }
    }
    public int Year
    {
        get { return TotalYears + 1; }
    }
    public int TotalDays
    {
        get { return days; }
    }
    public int TotalMonths
    {
        get { return (days / DaysPerMonth); }
    }
    public int TotalYears
    {
        get { return days / DaysPerYear; }
    }
    public int GetDaySpan(WierdDate other)
    {
        return other.days - days;
    }
    public override bool Equals(object obj)
    {
        return obj is WierdDate && ((WierdDate)obj) == this;
    }
    public override int GetHashCode()
    {
        return days.GetHashCode();
    }
    public override string ToString()
    {
        return string.Format("Day: {0}, Month: {1}, Year: {2}", Day, Month, Year);
    }
}
 
Share this answer
 
v2
So February 28th & 29th doesn't matter.

Tell that to the people born on those days.


but all months must be 30 days

Ummm, this isn't actually true. Some months have 30 days, and some have 31, and then there's that pesky February that you've already discounted. Are you now going to tell us that a solar year consists of fewer than 365 days?

Date math is a simple process in .Net. Just subtract the dates from each other into a TimeSpan object and play to your heart's content. How freakin' hard can it be?
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900