문제

저는 현재 ASP.Net C#으로 작은 달력을 작성하고 있습니다.현재 주의 행을 생성하기 위해 다음 for 루프를 수행합니다.

var iWeeks = 6;
for (int w = 0; w < iWeeks; w++) {

이것은 잘 작동하지만 어떤 달에는 5주만 있고, 드문 경우에는 4주만 있을 수도 있습니다.

특정 달에 필요한 행 수를 어떻게 계산할 수 있나요?

이것은 내가 만들고 있는 것의 예입니다:

enter image description here

그러나 위 달에서 볼 수 있듯이 필요한 행은 5개뿐입니다.토요일에 시작하여 6번째 주/행의 월요일에 끝나는 이번 달(2008년 8월)을 생각해 보세요.

구글에서 찾은 이미지


이것은 내가 만들고 있는 것의 예입니다:

enter image description here

그러나 위 달에서 볼 수 있듯이 필요한 행은 5개뿐입니다.토요일에 시작하여 6번째 주/행의 월요일에 끝나는 이번 달(2008년 8월)을 생각해 보세요.

구글에서 찾은 이미지

도움이 되었습니까?

해결책

이를 수행하는 방법은 다음과 같습니다.

public int GetWeekRows(int year, int month)
{
    DateTime firstDayOfMonth = new DateTime(year, month, 1);
    DateTime lastDayOfMonth = new DateTime(year, month, 1).AddMonths(1).AddDays(-1);
    System.Globalization.Calendar calendar = System.Threading.Thread.CurrentThread.CurrentCulture.Calendar;
    int lastWeek = calendar.GetWeekOfYear(lastDayOfMonth, System.Globalization.CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
    int firstWeek = calendar.GetWeekOfYear(firstDayOfMonth, System.Globalization.CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
    return lastWeek - firstWeek + 1;
}

System.Globalization.CalendarWeekRule.FirstFourDayWeek 부분을 수정하여 달력 주 규칙을 사용자 정의할 수 있습니다.코드가 자명하기를 바랍니다.

다른 팁

글쎄, 사용하는 문화권에 따라 다르지만 Thread.CurrentThread.CurrentCulture를 사용할 수 있다고 가정하면 오늘의 주를 가져오는 코드는 다음과 같습니다.

Culture culture = Thread.CurrentThread.CurrentCulture;
Calendar cal = culture.Calendar;
Int32 week = cal.GetWeekOfYear(DateTime.Today,
    culture.DateTimeFormat.CalendarWeekRule,
    culture.DateTimeFormat.FirstDayOfWeek);

첫 번째 날과 마지막 날이 몇 주에 속하는지 확인하는 것은 어떨까요?

확인하다 Calendar.GetWeekOfYear.그것은 트릭을 수행해야합니다.

ISO 8601의 4일 규칙을 따르지 않는다는 문제가 있지만 그 외에는 깔끔합니다.

DateTime.DaysInMonth(int whichYear,int whichMonth)를 사용하여 한 달의 날짜를 얻을 수 있습니다.

율리우스력/그레고리력의 달은 매년 동일한 일수를 갖습니다. 단, 2월은 연도 윤년에 따라 28일 또는 29일이 될 수 있습니다.설명 섹션에서 일수를 확인할 수 있습니다. http://en.wikipedia.org/wiki/Gregorian_calendar.

@darkdog이 언급했듯이 DateTime.DaysInMonth가 있습니다.이렇게 하세요:

var days = DateTime.DaysInMonth(year, month) + 
           WhatDayOfWeekTheMonthStarts(year, month);    
int rows = (days / 7);  
if (0 < days % 7)  
{  
    ++rows;
}  

세계화/현지화 목적을 위해 세계 일부 지역에서는 다양한 달력/연도 구성 방법을 사용한다는 사실을 고려하십시오.

문제는 한 달의 날짜 수가 아니라 몇 주에 걸쳐 지속되는지입니다.

윤년이 아닌 해의 2월은 28일로 구성되며, 해당 월의 1일이 월요일인 경우 2월은 정확히 4주로 구성됩니다.

그러나 해당 월의 1일이 화요일이거나 다른 요일인 경우 2월은 5주로 구성됩니다.

31일이 있는 한 달은 같은 방식으로 5주 또는 6주에 걸쳐 있을 수 있습니다.월이 월요일에 시작하면 31일이 5주 숫자가 됩니다.해당 월이 토요일이나 일요일에 시작하는 경우 6주로 구성됩니다.

따라서 이 숫자를 얻는 올바른 방법은 해당 월의 첫 번째 날과 마지막 날의 주 번호를 찾는 것입니다.


편집 #1:특정 달의 주 수를 계산하는 방법은 다음과 같습니다.편집 #2:코드의 버그를 수정했습니다.

public static Int32 GetWeekForDateCurrentCulture(DateTime dt)
{
    CultureInfo culture = Thread.CurrentThread.CurrentCulture;
    Calendar cal = culture.Calendar;
    return cal.GetWeekOfYear(dt,
        culture.DateTimeFormat.CalendarWeekRule,
        culture.DateTimeFormat.FirstDayOfWeek);
}

public static Int32 GetWeekSpanCountForMonth(DateTime dt)
{
    DateTime firstDayInMonth = new DateTime(dt.Year, dt.Month, 1);
    DateTime lastDayInMonth = firstDayInMonth.AddMonths(1).AddDays(-1);
    return
        GetWeekForDateCurrentCulture(lastDayInMonth)
        - GetWeekForDateCurrentCulture(firstDayInMonth)
        + 1;
}

이 시도,

DateTime.DaysInMonth

먼저 매월 1일이 어느 평일인지 알아보세요.첫날이 항상 1이고 해당 연도와 월이 포함된 날짜/시간을 새로 설정하면 요일 속성이 있습니다.

그런 다음 여기에서 해당 월의 일 수를 사용할 수 있습니다. DateTime.DaysInMonth, 7로 나눈 다음 첫 번째 날이 속하는 일수를 1에서 더하는 주 수를 결정하기 위해.예를 들어,

public static int RowsForMonth(int month, int year)
{
    DateTime first = new DateTime(year, month, 1);

    //number of days pushed beyond monday this one sits
    int offset = ((int)first.DayOfWeek) - 1;

    int actualdays = DateTime.DaysInMonth(month, year) +  offset;

    decimal rows = (actualdays / 7);
    if ((rows - ((int)rows)) > .1)
    {
        rows++;
    }
    return rows;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top