失忆的硬盘

如何得到指定周是几号到几号?
/// <summary>
 2        /// 计算输入时间是第几周.开始时间和结束时间
 3        /// </summary>
 4        /// <param name="datetime">计算的时间</param>
 5        /// <param name="Week">返回第几周</param>
 6        /// <param name="begTime">返回开始时间</param>
 7        /// <param name="endTime">返回结束时间</param>
 8        /// <param name="Format">时间格式化</param>
 9        private void GetWeekDay(DateTime datetime,ref string Week,ref string begTime,ref string endTime,string Format)
10        {
11            //
12            Week = (datetime.DayOfYear / 7 + 1).ToString();
13
14            int dayOfWeek = -(int)datetime.DayOfWeek;
15            DateTime firstDayOfWeek;
16            if (dayOfWeek == 0)
17            {
18                firstDayOfWeek = datetime.AddDays(-7);
19            }

20            else
21            {
22                firstDayOfWeek = datetime.AddDays(-(int)datetime.DayOfWeek);
23            }

24            if (Format == null || Format.Length <= 0)
25                Format = "yyyy-MM-dd";
26            //开始时间 星期日开始
27            begTime = firstDayOfWeek.ToString(Format);
28            //结束时间
29            endTime = firstDayOfWeek.AddDays(6).ToString(Format);
30        }

31        /**//// <summary>
32        /// 根据输入年份和周期得到
33        /// </summary>
34        /// <param name="Year">年份</param>
35        /// <param name="Week">周期</param>
36        /// <param name="begTime">返回开始时间</param>
37        /// <param name="endTime">返回结束时间</param>
38        /// <param name="Format">返回日期格式 yyyy-MM-dd</param>
39        private void GetWeekDay(string Year, string Week, ref string begTime, ref string endTime, string Format)
40        {
41            DateTime dt = new DateTime();
42            dt = Convert.ToDateTime(Year + "-01-01");
43            int week = Convert.ToInt32(Week);
44            dt = dt.AddDays((week - 1* 7);
45            int dayOfWeek = -(int)dt.DayOfWeek;
46            DateTime firstDayOfWeek;
47            if (dayOfWeek == 0)
48            {
49                firstDayOfWeek = dt.AddDays(-7);
50            }

51            else
52            {
53                firstDayOfWeek = dt.AddDays(-(int)dt.DayOfWeek);
54            }

55            if (Format == null || Format.Length <= 0)
56                Format = "yyyy-MM-dd";
57            //开始时间 星期日开始
58            begTime = firstDayOfWeek.ToString(Format);
59            //结束时间
60            endTime = firstDayOfWeek.AddDays(6).ToString(Format);
61        }

posted on 2008-07-16 10:53  失忆的硬盘  阅读(756)  评论(0编辑  收藏  举报