关闭

.NET中根据日起求周别,或者根据周别求日期的方法

标签: .netstringcalendarobjectsystemclass
1486人阅读 评论(0) 收藏 举报
分类:

以下方法供大家参考,若想返回日期类型,自己修改.

using System;
using System.Web.UI;
using System.Globalization;

public partial class CSDateTime : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("今天是第几周? -- ");
        Response.Write(GetWeekOfYear(DateTime.Now));
        Response.Write("<br>");
        Response.Write("第200602周的日期起讫? -- ");
        Response.Write(GetWeekFirstDay("200602"));
        Response.Write(" - ");
        Response.Write(GetWeekLastDay("200602"));
        Response.Write("<br>");
        Response.Write("第200622周包含哪些日期? -- ");
        string[] day = GetWeekRange("200622");
        for (int i = 0; i < 7;i++ )
        {
            Response.Write(day[i] + " ");
        }
    }
   
    /// <summary>
    /// 求指定日期的周别,周别的第一天为星期天
    /// </summary>
    /// <param name="dt">某一天</param>
    /// <returns>返回的周别格式如200601,表示2006年度第一周</returns>
    private string GetWeekOfYear(DateTime dt)
    {
        CultureInfo ci = CultureInfo.CurrentCulture;
        Calendar calendar = ci.Calendar;
        CalendarWeekRule rule = ci.DateTimeFormat.CalendarWeekRule;
        DayOfWeek dow = ci.DateTimeFormat.FirstDayOfWeek;
        int week = calendar.GetWeekOfYear(dt, rule, dow);
        string weekId = dt.Year + week.ToString("00");
        return weekId;
    }
   
    /// <summary>
    /// 求指定周别的第一天
    /// </summary>
    /// <param name="weekId">周别,格式如200601</param>
    /// <returns>返回日期的yyyy-MM-dd字串</returns>
    private string GetWeekFirstDay(string weekId)
    {
        int year = int.Parse(weekId.Substring(0,4));
        DateTime newYearDay = new DateTime(year,1,1);
        int firstweekfirstday = Convert.ToInt32(newYearDay.DayOfWeek);
        int days = (int)(7-firstweekfirstday);
        DateTime secondweekfisrtday = newYearDay.AddDays(days);
        int week = int.Parse(weekId.Substring(4, 2));
        string firstdate = secondweekfisrtday.AddDays((week - 2) * 7).ToString("yyyy-MM-dd");
        return firstdate;
    }
   
    /// <summary>
    /// 求指定周别的最后一天
    /// </summary>
    /// <param name="weekId">周别,格式如200601</param>
    /// <returns>返回日期的yyyy-MM-dd字串</returns>
    private string GetWeekLastDay(string weekId)
    {
        int year = int.Parse(weekId.Substring(0, 4));
        DateTime newYearDay = new DateTime(year, 1, 1);
        int firstweekfirstday = Convert.ToInt32(newYearDay.DayOfWeek);
        int days = (int)(7 - firstweekfirstday);
        DateTime secondweekfisrtday = newYearDay.AddDays(days);
        int week = int.Parse(weekId.Substring(4, 2));
        string lastdate = secondweekfisrtday.AddDays((week - 2) * 7+6).ToString("yyyy-MM-dd");
        return lastdate;
    }
   
    /// <summary>
    /// 求指定周别的日期范围
    /// </summary>
    /// <param name="weekId">周别,格式如200601</param>
    /// <returns>返回日期的yyyy-MM-dd字串</returns>
    private string[] GetWeekRange(string weekId)
    {
        string[] day = new string[7];
        int year = int.Parse(weekId.Substring(0, 4));
        DateTime newYearDay = new DateTime(year, 1, 1);
        int firstweekfirstday = Convert.ToInt32(newYearDay.DayOfWeek);
        int days = (int)(7 - firstweekfirstday);
        DateTime secondweekfisrtday = newYearDay.AddDays(days);
        int week = int.Parse(weekId.Substring(4, 2));
        DateTime firstdate = secondweekfisrtday.AddDays((week - 2) * 7);
        DateTime lastdate = secondweekfisrtday.AddDays((week - 2)*7 + 6);
        day[0] = firstdate.ToString("yyyy-MM-dd");
        for (int i=1;i<=5;i++)
        {
            day[i] = firstdate.AddDays(i).ToString("yyyy-MM-dd");
        }
        day[6] = lastdate.ToString("yyyy-MM-dd");
        return day;
    }

}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:218544次
    • 积分:3613
    • 等级:
    • 排名:第9463名
    • 原创:102篇
    • 转载:2篇
    • 译文:0篇
    • 评论:97条
    文章分类
    最新评论