How do I get the number of weeks in a date range in C #?

3

I would like to know how to know the number of weeks in a given date range in C #.

    
asked by anonymous 17.02.2014 / 21:03

5 answers

3

There are several ways, depending on what you are considering as a week.

Entire Calendar Weeks

Considering entire weeks of the calendar, ie only calendar weeks that are fully inserted in the range are counted:

public static int SemanasInteirasDoCalendario(DateTime d1, DateTime d2)
{
    if (d2 < d1) throw new ArgumentException(
        "'d1' precisa ser menor ou igual que 'd2'");
    return ((d2 - d1).Days + 1
        - ((int)d1.DayOfWeek > 0 ? 6 - (int)d1.DayOfWeek : 0)
        - ((int)d2.DayOfWeek < 6 ? (int)d2.DayOfWeek : 0)) / 7;
}

Calendar partial weeks

Consider any calendar week, where at least one day of this week is within the range:

public static int SemanasParciaisDoCalendario(DateTime d1, DateTime d2)
{
    return SemanasInteirasDoCalendario(d1, d2)
        + ((int)d1.DayOfWeek > 0 ? 1 : 0)
        + ((int)d2.DayOfWeek < 6
            && d1.Date.AddDays(-(int)d1.DayOfWeek) != d2.Date.AddDays(-(int)d2.DayOfWeek)
            ? 1 : 0);
}

Entire weeks

Consider integer intervals of 7 consecutive days that are within the range of past dates:

public static int SemanasInteiras(DateTime d1, DateTime d2)
{
    if (d2 < d1) throw new ArgumentException(
        "'d1' precisa ser menor ou igual que 'd2'");
    return ((d2 - d1).Days + 1) / 7;
}

Partial weeks

Consider 7-day intervals where at least one of the days appears within the date range:

public static int SemanasParciais(DateTime d1, DateTime d2)
{
    if (d2 < d1) throw new ArgumentException(
        "'d1' precisa ser menor ou igual que 'd2'");
    var days = (d2 - d1).Days + 1;
    return days / 7 + Math.Sign(days % 7);
}

Entire weeks of work

Consider only whole weeks of work (Monday through Friday) that are inserted within the last range:

public static int SemanasInteirasDeTrabalho(DateTime d1, DateTime d2)
{
    if (d2 < d1) throw new ArgumentException(
        "'d1' precisa ser menor ou igual que 'd2'");
    return ((d2 - d1).Days + 1
        - ((int)d1.DayOfWeek > 1 ? 6 - (int)d1.DayOfWeek : -(int)d1.DayOfWeek)
        - ((int)d2.DayOfWeek < 5 ? (int)d2.DayOfWeek : (int)d2.DayOfWeek - 6)) / 7;
}

Partial weeks of work

Consider any work week (Monday through Friday) where at least one of the days is within the last date range:

public static int SemanasParciaisDeTrabalho(DateTime d1, DateTime d2)
{
    return SemanasInteirasDeTrabalho(d1, d2)
        + ((int)d1.DayOfWeek > 1 && (int)d1.DayOfWeek < 6 ? 1 : 0)
        + ((int)d2.DayOfWeek < 5 && (int)d2.DayOfWeek > 0
            && d1.Date.AddDays(-(int)d1.DayOfWeek) != d2.Date.AddDays(-(int)d2.DayOfWeek)
            ? 1 : 0);
}
    
18.02.2014 / 17:01
1

I could try this way:

public int NumeroDeSemanas(DateTime dataInicial, DateTime dataFinal)
{
   TimeSpan Span = dataFinal.Subtract(dataInicial);

   //Testa se a diferença é menor ou igual a 7 (número de dias em uma semana)
   if (Span.Days <= 7)
   {
      if (dataInicial.DayOfWeek > dataFinal.DayOfWeek)
      {
         return 2;
      }

      return 1;
   }

   int Dias = Span.Days - 7 + (int)dataInicial.DayOfWeek;
   int SemanasCount = 1;
   int DiasCount = 0;

   for (SemanasCount = 1; DiasCount < Dias; SemanasCount++)
   {
      DiasCount += 7;
   }

   return SemanasCount;
}

As a parameter to this method you pass the start date and end date, and the method returns the number of weeks.

    
17.02.2014 / 21:12
1

Try to use this, simpler:

    public int totalSemanas(DateTime dataInicial, DateTime dataFim)
    {
        return (dataFim - dataInicial).Days / 7;
    }
    
17.02.2014 / 21:35
0

Try this method:

public int totalSemanas(DateTime dataInicial, DateTime dataFim)
{
    return (int)Math.Ceiling(((dataFim- dataInicial).TotalDays + 1) / 7d);
}

Or this:

public int GetCalendarWeeks(DateTime startDate, DateTime endDate)
{
    return (endDate.GetBeginningOfWeek().Subtract(startDate.GetBeginningOfWeek()).Days / 7) + 1;
}

public static DateTime GetBeginningOfWeek(this DateTime date)
{
    var dayOfWeekOffSet = date.DayOfWeek == DayOfWeek.Sunday ? 6 : ((int)date.DayOfWeek) - 1;
    return date.AddDays(-1 * dayOfWeekOffSet).Date;
}

public static DateTime GetEndOfWeek(this DateTime date)
{
    var dayOfWeekOffSet = date.DayOfWeek == DayOfWeek.Sunday ? 0 : 7 - ((int)date.DayOfWeek);
    return date.AddDays(dayOfWeekOffSet).Date;
}
    
18.02.2014 / 13:24
0

In the there is the function dateDiff that assists you in this. And to use it in C #, just add the reference "Microsoft.VisualBasic" to your project, and include namespace in your class:

using Microsoft.VisualBasic;

The following code example:

        DateTime x = DateTime.Today.AddDays(-31);
        DateTime y = DateTime.Today;

        long l = DateAndTime.DateDiff(DateInterval.WeekOfYear, x, y);
        long ll = DateAndTime.DateDiff(DateInterval.Weekday, x, y);
    
18.02.2014 / 17:13