如何根据所选日期获取一个月的星期数? [英] How to get the week number of a month based on the selected date?

查看:126
本文介绍了如何根据所选日期获取一个月的星期数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想根据在asp.net日历控件中选择的日期来获取标签中月份的星期数.我不想按年明智的周数,我想按月明智的.例如,本月需要6周,因此,我希望根据所选日期显示1到6周.

Hi,

I want to get the week number of the month in the label based on the date selected in the asp.net calender control. I dont want week numbers by yearly wise, I want monthly wise. for example this month contails six weeks so , I want weeks from 1 to 6 to be displayed based on the selected date.

推荐答案

尝试一下(扩展方法)

Try this one (an extension method)

/// <summary>
/// Gets the week number for a provided date time value based on the current culture settings.
/// </summary>
/// <param name="dateTime">The date time.</param>
/// <returns>The week number</returns>
public static int GetWeekOfYear(this DateTime dateTime) {
    var culture = CultureInfo.CurrentUICulture;
    var calendar = culture.Calendar;
    var dateTimeFormat = culture.DateTimeFormat;

    return calendar.GetWeekOfYear(dateTime, dateTimeFormat.CalendarWeekRule, dateTimeFormat.FirstDayOfWeek);
}




由于您的问题尚不清楚,如果有的话,请查看此链接.

http://msdn.microsoft.com/en-us/library/system. globalization.calendar.getweekofyear.aspx [ ^ ]

欢呼




As your question is not clear, if any, check this link out.

http://msdn.microsoft.com/en-us/library/system.globalization.calendar.getweekofyear.aspx[^]

Cheers


引用该线程

http://stackoverflow.com/questions/2136487/calculate-week- of-month-in-net/2136549#2136549 [ ^ ]
Refer this thread

http://stackoverflow.com/questions/2136487/calculate-week-of-month-in-net/2136549#2136549[^]


尝试一下

Try this

public static int WeekDay(DateTime dt) 
        { 
            // Set Year 
            int yyyy = dt.Year; 
            // Set Month 
            int mm = dt.Month; 
 
            // Set Day 
            int dd = dt.Day; 
            // Declare other required variables 
            int DayOfYearNumber; 
            int Jan1WeekDay; 
            int WeekDay; 
 
            int i, j, k, l; 
            int[] Mnth = new int[12] { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }; 
 
            // Set DayofYear Number for yyyy mm dd 
            DayOfYearNumber = dd + Mnth[mm - 1]; 
            // Increase of Dayof Year Number by 1, if year is leapyear and month is february 
            if ((IsLeapYear(yyyy) == true) && (mm == 2)) 
                DayOfYearNumber += 1; 
            // Find the Jan1WeekDay for year 
            i = (yyyy - 1) % 100; 
            j = (yyyy - 1) - i; 
            k = i + i / 4; 
            Jan1WeekDay = 1 + (((((j / 100) % 4) * 5) + k) % 7); 
            // Calcuate the WeekDay for the given date 
            l = DayOfYearNumber + (Jan1WeekDay - 1); 
            WeekDay = 1 + ((l - 1) % 7); 
            return WeekDay; 
        } 
 
        public static bool IsLeapYear(int yyyy) 
        { 
 
            if ((yyyy % 4 == 0 && yyyy % 100 != 0) || (yyyy % 400 == 0)) 
                return true; 
            else 
                return false; 
        }  



http://social.msdn.microsoft.com/Forums/zh_CN/csharpgeneral/thread/bf504bba-85cb-492d-a8f7-4ccabdf882cb [



http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/bf504bba-85cb-492d-a8f7-4ccabdf882cb[^]


这篇关于如何根据所选日期获取一个月的星期数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆