How to Get the Start and End Date of a Week having the number of the week?

0

I have the number of the week, I want to get the date that includes that week:

Example: The week starts on Monday (first day), and ends on Sunday

  • Week 1 of 2018 = 01/01/2018 - 07/01/2018
  • Week 52 of 2018 = 12/24/2018 - 12/30/2018
  • Week 1 of 2019 = 12/31/2018 - 06/01/2019
asked by byok22 03.01.2018 в 23:41
source

2 answers

2

Several types of aspects have to be worked on to generate this sentence based on a date.

string setRangoSemana(DateTime fecha)
        {
            var noSemana = numeroSemana(fecha);
            //Validación para verificar si es la ultima semana del anio
            var semana = primerDíaSemana(noSemana == 52 ? fecha.AddYears(-1).Year : fecha.Year, noSemana, CultureInfo.CurrentCulture);
            var lunes = semana.AddDays(1);
            var domingo = semana.AddDays(7);
            return string.Format("Semana {0} de {1} = {2}-{3}", noSemana, fecha.Year, lunes.ToShortDateString(), domingo.ToShortDateString());
        }

Having the week number Weekly number () I will go to get the first day firstDayWeek () based on the year and the number of the week this returns you the first day of the week, in this case Sunday.

int numeroSemana(DateTime time)
    {
        DayOfWeek day = CultureInfo.InvariantCulture.Calendar.GetDayOfWeek(time);
        if (day >= DayOfWeek.Monday && day <= DayOfWeek.Wednesday)
        {
            time = time.AddDays(3);
        }
        return CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(time, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
    }
static DateTime primerDíaSemana(int year, int weekOfYear, System.Globalization.CultureInfo ci)
        {
            DateTime jan1 = new DateTime(year, 1, 1);
            int daysOffset = (int)ci.DateTimeFormat.FirstDayOfWeek - (int)jan1.DayOfWeek;
            DateTime firstWeekDay = jan1.AddDays(daysOffset);
            int firstWeek = ci.Calendar.GetWeekOfYear(jan1, ci.DateTimeFormat.CalendarWeekRule, ci.DateTimeFormat.FirstDayOfWeek);
            if ((firstWeek <= 1 || firstWeek >= 52) && daysOffset >= -3)
            {
                weekOfYear -= 1;
            }
            return firstWeekDay.AddDays(weekOfYear * 7);
        }

And the rest of the days the sums with the function AddDays ()

    
answered by 03.01.2018 в 23:57
0

Due to your little research, I will place this RESPONSE of the user @ Tim Schmelter based on a similar question in OS in English .

Explicit translation (Original Answer):

  

You can use the following two methods to calculate the week number and start date of a given week number according to a given year:

// este método es tomado de http://stackoverflow.com/a/11155102/284240
public static int GetIso8601WeekOfYear(DateTime time)
{
    DayOfWeek day = CultureInfo.InvariantCulture.Calendar.GetDayOfWeek(time);
    if (day >= DayOfWeek.Monday && day <= DayOfWeek.Wednesday)
    {
        time = time.AddDays(3);
    }

    return CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(time, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
} 

public static DateTime FirstDateOfWeek(int year, int weekOfYear, System.Globalization.CultureInfo ci)
{
    DateTime jan1 = new DateTime(year, 1, 1);
    int daysOffset = (int)ci.DateTimeFormat.FirstDayOfWeek - (int)jan1.DayOfWeek;
    DateTime firstWeekDay = jan1.AddDays(daysOffset);
    int firstWeek = ci.Calendar.GetWeekOfYear(jan1, ci.DateTimeFormat.CalendarWeekRule, ci.DateTimeFormat.FirstDayOfWeek);
    if ((firstWeek <= 1 || firstWeek >= 52) && daysOffset >= -3)
    {
        weekOfYear -= 1;
    }
    return firstWeekDay.AddDays(weekOfYear * 7);
}
  

Then you can get both dates as follows:

// 46
int thisWeekNumber = GetIso8601WeekOfYear(DateTime.Today); 
// 11/11/2013  
DateTime firstDayOfWeek= FirstDateOfWeek(2013, thisWeekNumber, CultureInfo.CurrentCulture); 
// 11/12/2012  
DateTime firstDayOfLastYearWeek = FirstDateOfWeek(2012, thisWeekNumber, CultureInfo.CurrentCulture); 
  

Add 6 days to arrive at the end of the week.

The code is not complicated and it is easy to understand this should help you.

Greetings.!

    
answered by 05.01.2018 в 14:58