2

Lets say, I have two date Order date - 1/1/2014 and Delivery date - 6/2/2014. Now if I want to calculate how much work week its taken (Order date-delivery date), how can I do it in c#.

Nagaraj S
  • 13,316
  • 6
  • 32
  • 53
Gulrej
  • 969
  • 4
  • 15
  • 25
  • 4
    Provide more details. How do you store dates? What have you already tried? – rebeliagamer Feb 06 '14 at 08:44
  • 1
    http://stackoverflow.com/questions/4604199/how-to-calculate-number-of-weeks-given-2-dates – Nagaraj S Feb 06 '14 at 08:45
  • I have tried like this - (Delivary date - Order date).TotalDays/7. And I am storing the output as int – Gulrej Feb 06 '14 at 08:48
  • 1
    Wouldn't the nr. of "work weeks" be the same as the nr. of normal weeks? How are they different? – Magnus Feb 06 '14 at 08:50
  • @Magnus consider Friday to Monday. Which is half a week but 2 workweeks – toATwork Feb 06 '14 at 08:52
  • @toATwork Friday to Monday is two "works weeks"? What is the definition for a "work week"? – Magnus Feb 06 '14 at 08:54
  • @Magnus that is exactly what needs more clarification. I assumed Friday to Monday are 2 work week because Friday was e.g. in calender week 3 and Friday in caleder week 4 - so 2 weeks of work – toATwork Feb 06 '14 at 08:56

5 Answers5

1

If you want the number of worked days in a date range, you can use this:

        var from = DateTime.Today.AddDays(-10);
        var to = DateTime.Today;
        var daysOfWeek = new DayOfWeek[] { DayOfWeek.Monday, DayOfWeek.Tuesday
                                          , DayOfWeek.Wednesday, DayOfWeek.Friday
                                          , DayOfWeek.Thursday };


        var days = Enumerable.Range(0, 1 + to.Subtract(from).Days)
                             .Select((n, i) => from.AddDays(i).DayOfWeek)
                             .Where(n => daysOfWeek.Contains(n.DayOfWeek));

If you want the number of weeks during a date range, use this:

        (int)((to - from).TotalDays/7)
AlexH
  • 2,650
  • 1
  • 27
  • 35
  • Wow, that is a brilliant solution! I'm writing this one down for sure. I've tested it and it works like a charm. – B.K. Feb 06 '14 at 09:08
0

I am presuming by "how much workweek" you mean "how many workdays". This is not so straightforward as it depends on the culture and you need to take holidays into account.

If you rely on Mon through Fri being the work days you could use a solution similar to what was discussed in c# DateTime to Add/Subtract Working Days, counting each day from Order Date to Delivery Date for which the conditions hold.

That Q&A still leaves you with the issue of how to determine the holidays of a certain region (be warned - in Switzerland each part of the country has different holidays!).

Update: From Nagaraj's suggested link I gather that you might also refer to "weeks" as chunks (that is "how many workweeks it has taken"). If so, in turn, you will need to define how many days of a week must be taken to take the week into account...

Community
  • 1
  • 1
chiccodoro
  • 14,407
  • 19
  • 87
  • 130
0
(int)((DeliveryDate-OrderDate).TotalDays/7)
Reza ArabQaeni
  • 4,848
  • 27
  • 46
0

I guess you are not interested in working days but weeks. You can use GetWeekOfYear:

http://msdn.microsoft.com/en-us/library/system.globalization.calendar.getweekofyear%28v=vs.110%29.aspx

EDIT

To respond to the comment, here some code example:

int start = System.Globalization.CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(new DateTime(2014, 1, 14), System.Globalization.CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
int end = System.Globalization.CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(new DateTime(2014, 2, 3), System.Globalization.CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
int weeks = end - start;

That should give you the weeks needed.

toATwork
  • 1,335
  • 16
  • 34
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Michael Schmidt Feb 06 '14 at 09:07
  • @dTDesign didn't thought of the possibility that Microsoft might change the address - you are totally corret. I'll keep it in mind for future answers – toATwork Feb 06 '14 at 10:09
0

I'm using strings and convert that to dates, because I'm not sure where you get your dates and in what form. Adjust your code accordingly.

string orderDate = @"1/1/2014";
string deliveryDate = @"6/2/2014";

// This will give you a total number of days that passed between the two dates.
double daysPassed = Convert.ToDateTime(deliveryDate).
                    Subtract(Convert.ToDateTime(orderDate)).TotalDays;

// Use this if you want actual weeks.  This will give you a double approximate.  Change to it to an integer round it off (truncate it).
double weeksPassed = daysPassed / 7;

// Use this if you want to get an approximate number of work days in those weeks (based on 5 days a week schedule).
double workDaysPassed = weeksPassed * 5;
B.K.
  • 9,982
  • 10
  • 73
  • 105