asp.net关于时间方法,当前月有多少天、求某年有多少周、求当前日期是一年的中第几周

来源:互联网 发布:moka软件下载 编辑:程序博客网 时间:2024/05/01 21:09
1.某个年月有多少天
HowMonthDay
 /// 某个年月有多少天 
    
/// </summary> 
    
/// <param name="y"></param> 
    
/// <param name="m"></param> 
    
/// <returns></returns> 

    public static int HowMonthDay(int y, int m)
    {
        int mnext;
        int ynext;
        if (m < 12)
        {
            mnext = m + 1;
            ynext = y;
        }
        else
        {
            mnext = 1;
            ynext = y + 1;
        }
        DateTime dt1 = System.Convert.ToDateTime(y + "-" + m + "-1");
        DateTime dt2 = System.Convert.ToDateTime(ynext + "-" + mnext + "-1");
        TimeSpan diff = dt2 - dt1;
        return diff.Days;
    } 
 

2.某年有多少周

GetYearWeekCount 

    /// <summary> 
    
/// 求某年有多少周 
    
/// 返回 int 
    
/// </summary> 
    
/// <param name="strYear"></param> 
    
/// <returns>int</returns> 
    public static int GetYearWeekCount(int strYear)
    {
        System.DateTime fDt = DateTime.Parse(strYear.ToString() + "-01-01");
        int k = Convert.ToInt32(fDt.DayOfWeek);//得到该年的第一天是周几 
        if (k == 1)
        {
            int countDay = fDt.AddYears(1).AddDays(-1).DayOfYear;
            int countWeek = countDay / 7 + 1;
            return countWeek;

        }
        else
        {
            int countDay = fDt.AddYears(1).AddDays(-1).DayOfYear;
            int countWeek = countDay / 7 + 2;
            return countWeek;
        }

    } 

3.某日期是一年的中第几周

 WeekOfYear

    /// <summary> 
    
/// 求某日期是一年的中第几周 
    
/// </summary> 
    
/// <param name="date"></param> 
    
/// <returns></returns> 
    public static int WeekOfYear(DateTime curDay)
    {
        int firstdayofweek = Convert.ToInt32(Convert.ToDateTime(curDay.Year.ToString() + "" + "1-1 ").DayOfWeek);

        int days = curDay.DayOfYear;
        int daysOutOneWeek = days - (7 - firstdayofweek);

        if (daysOutOneWeek <= 0)
        {
            return 1;
        }
        else
        {
            int weeks = daysOutOneWeek / 7;
            if (daysOutOneWeek % 7 != 0)
                weeks++;

            return weeks + 1;
        }
    }