none
Yılın Haftalarının Hangi Tarihler Arasına Geldiğini Bulma RRS feed

  • Soru

  • Merhabalar,

    Araştırdım ama soruma cevap bulamadım internette.

    İstediğim çok açık bir şey.

    Bir yıla ait bütün haftaları ve o haftaların tarih aralıklarını gösterecek.

    Örneğin

    Yıl Hafta No Başlangıç Tarihi  Bitiş Tarihi

    2015 2 07.01.2015 13.01.2015

    2015 3 14.01.2015 20.01.2015

    bu şekilde bir yıla ait bütün haftaları çekebilmek ?

    26 Eylül 2015 Cumartesi 12:10

Yanıtlar

  • void Main()
    {
    	var haftalar = GetWeeks(new DateTime(2015, 1, 1), 60);
    }
    private List<Week> GetWeeks(DateTime start, int weeks)
    {
        var firstMonday = GetMonday(start);
    	
    	return (from w in Enumerable.Range( 0, weeks )
    	let monday = firstMonday.AddDays( w * 7 )
    	select  new Week { 
    		Year = monday.Year,
    		WeekNo = GetIso8601WeekOfYear(monday),
    		Start = monday,
    		End = monday.AddDays(6)
    		} )
    	.ToList();
    	
    }
    
    private DateTime GetMonday(DateTime day)
    {
    	return day.AddDays( day.DayOfWeek == DayOfWeek.Sunday ? -6 : 1-(int)day.DayOfWeek );	
    }
    
    private List<Week> GetWeekDaysForYear(int year)
    {
        var fdoy = new DateTime(year, 1, 1);
    	var ldoy = new DateTime(year, 12, 31);
    	
        var weeks = Enumerable.Range(0,366).Select (e => fdoy.AddDays(e)).Where (e => e.Year == year)
    	.GroupBy (d => GetIso8601WeekOfYear(d))
    	.OrderBy (d => d.Key);
    	
    	var days = from w in weeks
    	let firstDay = w.Min ()
    	let lastDay = w.Max()
    	let fdow = firstDay != fdoy ? firstDay : lastDay.AddDays(-6)
    	let ldow = lastDay  != ldoy ? lastDay  : firstDay.AddDays(6) 
    	select new Week { Year=year, WeekNo= w.Key, Start= fdow, End = ldow };
    
    	return days.ToList();
    }
    
    // Based on Shawn Steel's code on MSDN blog
    // http://blogs.msdn.com/b/shawnste/archive/2006/01/24/iso-8601-week-of-year-format-in-microsoft-net.aspx
    
    // This presumes that weeks start with Monday.
    // Week 1 is the 1st week of the year with a Thursday in it.
    public static int GetIso8601WeekOfYear(DateTime time)
    {
        // Seriously cheat.  If its Monday, Tuesday or Wednesday, then it'll 
        // be the same week# as whatever Thursday, Friday or Saturday are,
        // and we always get those right
        DayOfWeek day = CultureInfo.InvariantCulture.Calendar.GetDayOfWeek(time);
        if (day >= DayOfWeek.Monday && day <= DayOfWeek.Wednesday)
        {
            time = time.AddDays(3);
        }
    
        // Return the week of our adjusted day
        return CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(time, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
    } 
    
    public class Week
    {
    	public int Year { get; set; }
    	public int WeekNo { get; set; }
    	public DateTime Start { get; set; }
    	public DateTime End { get; set; }
    }

    • Yanıt Olarak İşaretleyen Ömer Birekul 26 Eylül 2015 Cumartesi 14:22
    26 Eylül 2015 Cumartesi 14:18