Hafta Başı ve Hafta Sonu Hesaplama

by Volem July 04, 2011 13:06

Gecenlerde haftayi ve yili bilip de haftanin ilk ve son gununu hesaplama ihtiyacim duydum. Umarim sizin de isinize yarar..


public class CalendarHelper
{
    /// <summary>
    /// Calculates start and end date of a week for a specific year.
    /// </summary>
    /// <param name="year">Year</param>
    /// <param name="week">Week number</param>
    /// <param name="startDate">Calculated start date of week</param>
    /// <param name="endDate">Calculated end date of week</param>
    public static void StartEndDateOfWeek(int year, int week, out DateTime startDate, out DateTime endDate)
    {
        // First of January
        DateTime jan1 = new DateTime(year, 1, 1);
 
        // Calculation is done according to the first day of week is monday but includes the offset,
        // so it will be correct for sunday and other first day of week.
        int daysOffset = (int)CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek - (int)jan1.DayOfWeek;
        DateTime firstMonday = jan1.AddDays(daysOffset);
 
        // Find the first week.
        int firstWeek = CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(jan1, CultureInfo.CurrentCulture.DateTimeFormat.CalendarWeekRule,
            CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek);
 
        if (firstWeek <= 1)
        {
            week -= 1;
        }
        startDate = firstMonday.AddDays(week * 7);
        endDate = startDate.AddDays(6);            
    }
}

Tags: , , , ,

C#

Comments

Add comment




  Country flag
Click to change captcha
biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.6.0.0
Theme by Mads Kristensen | Modified by Volem

RSS
View Volkan Nazmi Metin's profile on LinkedIn

RevolverMap

Son Eklenen Yazılar

Yazar Hakkında

Sosyal, evli, çocuklu, karısını ve kızını çok seven, gezmeyi seven, spor yapmak isteyen bir mühendis