Rob
Rob

Reputation: 3574

How to substract two dates resulting in days or weeks?

I'm looking for a function in which I can enter 2 dates:

Starting like this:

         public static String getDaysOrWeeksFromDateDiff(DateTime first, DateTime second)
        {
    //this function will substract the date from the other and result days if < 8 and weeks if > 7

return "1 week";
        }

I've tried this function below, but it gives me a negative date (I've missed the year somehow in the calculation)

 public static String getDaysOrWeeksFromDateDiff(DateTime first, DateTime second)
    {
        var cal = new GregorianCalendar(GregorianCalendarTypes.Localized);
        var weeknr = cal.GetWeekOfYear(first, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
        var weeknr2 = cal.GetWeekOfYear(second, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
        String strReturnValue;
        if (weeknr == weeknr2)
        {
            var dagen = first.Day - second.Day;
            strReturnValue = (dagen == 0 ? "today" : (dagen == 1 ? "1 day" : dagen + " days"));
        }
        else
        {
            strReturnValue = (weeknr - weeknr2 == 1 ? "1 week" : weeknr - weeknr2 + " weeks");
        }
        return strReturnValue;
    }

Upvotes: 2

Views: 120

Answers (6)

Nilay Vishwakarma
Nilay Vishwakarma

Reputation: 3363

void func(DateTime t1, DateTime t2)
{
    days = (t2 - t1).TotalDays;
    weeks = days/7;
    year = weeks/52;
}

Upvotes: -1

Heinzi
Heinzi

Reputation: 172270

You can subtract dates directly:

TimeSpan difference = second - first;

This returns a TimeSpan object, which you can query easily, e.g.

if (difference.Days >= 8) ...

Converting the number of Days into weeks should be a trivial exercise since every week has exactly 7 days...

(Note that Days will give you the number of days as an integer, whereas TotalDays will include fractional days.)

Upvotes: 5

Kjartan
Kjartan

Reputation: 19111

For a length of time like this, you should use TimeSpan instead of DateTime, and then calculate the number of weeks from the property TotalDays.

Upvotes: 1

Biggles
Biggles

Reputation: 1345

You can use Time span class

    public static String getWeekOrMonthFromDateDiff(DateTime first, DateTime second)
    {
        var span = second - first;
        if (span.Days <= 7)
            return span.Days + " day(s)";
        else
            return span.Days / 7 + " week(s)"; 
    }

Upvotes: 2

Relax
Relax

Reputation: 283

I've done something like this some days ago:

private string GetTimeSpan(DateTime toDateTime, DateTime fromDateTime)
    {
        TimeSpan ts = toDateTime- fromDateTime;

        if (ts.Days < 0)
        {
            return "since " + ts.Days.ToString().Replace("-", string.Empty) + " Days";
        }
        else if (ts.Hours < 1)
        {
            return "in " + ts.Minutes + " Minutes";
        }
        else if (ts.Days < 1)
        {
            return "in " + ts.Hours + " Hours";
        }
        else if (ts.Days < 7)
        {
            return "in " + ts.Days + " Days";
        }
        else
        {
            return "in " + ts.Days / 7 + " Weeks";
        }
    }

Modify it the way you Need it :)

Upvotes: 1

Nikhil Agrawal
Nikhil Agrawal

Reputation: 48568

You can simply do

TimeSpan diff = first - second;

Then you can do

int weekCount = diff.TotalDays / 7;

Upvotes: 1

Related Questions