aps.net中几个常用的时间计算

  1      /// <summary>
  2      /// 获取周的数据信息
  3      /// </summary>
  4     /// <param name="argTime">为当前时间</param>
  5     private void GetWeekData(string argTime)
  6     {
  7         DateTime _thisTime = Convert.ToDateTime(argTime);
  8         DateTime dtWeekStart = new DateTime();
  9         DateTime dtWeekeEnd = new DateTime();
 10         DateTimeUtils.GetWeek(_thisTime.Year, DateTimeUtils.WeekOfYear(_thisTime), out dtWeekStart, out dtWeekeEnd);
 11 
 12         int start = dtWeekStart.Day;
 13         int end = dtWeekeEnd.Day;
 14         DataTable _thisTable = CreateWhereTable();
 15 
 16         TimeSpan ts = dtWeekeEnd.Subtract(dtWeekStart);
 17 
 18         int j = ts.Days;
 19         for (int i = ts.Days; i >= 0; i--)
 20         {
 21             DateTime _thisMyTime = dtWeekStart.AddDays(j);
 22             _thisTable = CreateWhereTableRow(_thisTable, _thisMyTime.ToString("yyyy-MM-dd"), "Week");
 23             j -= 1;
 24         }
 25 
 26         string _thisTempTime = _thisTime.ToString("yyyy-MM-dd");
 27         DataRow _thisRow = _thisTable.NewRow();
 28         foreach (DataRow _row in _thisTable.Rows)
 29         {
 30             if (_row["Date"].ToString().Trim().Equals(_thisTempTime))
 31             {
 32                 _thisRow["Date"= _row["Date"];
 33                 _thisRow["Class"= _row["Class"];
 34                 _thisTable.Rows.Remove(_row);
 35                 _thisTable.Rows.InsertAt(_thisRow, 0);
 36                 break;
 37             }
 38         }
 39         //_thisTable 为当前一周内所有日期(当然这一周是以星期日开始)
 40         GetData(_thisTable);//这里则根据一周信息绑定相应数据方法。
 41 
 42     }
 43 
 44 
 45     /// <summary>
 46     /// 获取月数据
 47     /// </summary>
 48     /// <param name="argDate"></param>
 49     private void GetMonthDate(string argDate)
 50     {
 51         DataTable _thisTable = CreateWhereTable();
 52         DateTime _thisTime = DateTimeUtils.FirstDayOfMonth(Convert.ToDateTime(argDate));//获取月的第一天
 53         DateTime _thisEndTime = DateTimeUtils.LastDayOfMonth(Convert.ToDateTime(argDate));//获取月的最后一天
 54         int FirstDay = DateTimeUtils.FirstDayOfMonth(Convert.ToDateTime(argDate)).Day;//获取月第一天的天数
 55         int LastDay = DateTimeUtils.LastDayOfMonth(Convert.ToDateTime(argDate)).Day;//获取月最后一天的天数
 56         _thisTable = CreateWhereTableRow(_thisTable, _thisEndTime.ToString(), "Month");
 57 
 58         for (int i = FirstDay; i < LastDay; i++)
 59         {
 60             _thisTable = CreateWhereTableRow(_thisTable, _thisEndTime.AddDays(-i).ToString("yyyy-MM-dd"), "Month");
 61         }
 62 
 63         string _thisTempTime = Convert.ToDateTime(argDate).ToString("yyyy-MM-dd");
 64         DataRow _thisRow = _thisTable.NewRow();
 65         foreach (DataRow _row in _thisTable.Rows)
 66         {
 67             if (_row["Date"].ToString().Trim().Equals(_thisTempTime))
 68             {
 69                 _thisRow["Date"= _row["Date"];
 70                 _thisRow["Class"= _row["Class"];
 71                 _thisTable.Rows.Remove(_row);
 72                 _thisTable.Rows.InsertAt(_thisRow, 0);
 73                 break;
 74             }
 75         }
 76 
 77         GetData(_thisTable);
 78     }
 79 
 80 
 81     //获取年的数据
 82     private void GetYearDate(string argDate)
 83     {
 84         DateTime _thisDt = Convert.ToDateTime(argDate);
 85         DataTable _thisTable = CreateWhereTable();
 86         int year = _thisDt.Year;
 87 
 88         string _thisStrDate = string.Empty;
 89         for (int i = 12; i > 0; i--)
 90         {
 91             _thisStrDate = year + "-" + i + "-01";
 92             _thisTable = CreateWhereTableRow(_thisTable, Convert.ToDateTime(_thisStrDate).ToString("yyyy-MM-dd"), "Year");
 93         }
 94 
 95         string _thisYear = _thisDt.Year.ToString();
 96         string _thisMonth = _thisDt.Month.ToString();
 97         //下面操作是将当前时间放到第一  其他时间顺便不改变
 98         string strFYear = Convert.ToDateTime(_thisYear + "-" + _thisMonth + "-01").ToString("yyyy-MM-dd");
 99 
100         DataRow _thisRow = _thisTable.NewRow();
101         foreach (DataRow _row in _thisTable.Rows)
102         {
103             if (_row["Date"].ToString().Trim().Equals(strFYear))
104             {
105                 _thisRow["Date"= _row["Date"];
106                 _thisRow["Class"= _row["Class"];
107                 _thisTable.Rows.Remove(_row);
108                 _thisTable.Rows.InsertAt(_thisRow, 0);
109                 break;
110             }
111         }
112 
113         GetData(_thisTable);
114     }
115 
116 
117 
118 
119 

下面将放入常用日期处理类(当然这些常用函数大多数都是从网上搜索,希望大家一起完善此类。如有侵犯原版请指出)

日程处理类
  1  public class DateTimeUtils
  2     {
  3         //当天与该周星期一相差的天数
  4         public static int getModdayNum(System.DayOfWeek dw)
  5         {
  6             int weeknow = Convert.ToInt32(dw);
  7             int moddayNum = (-1* weeknow + 1;
  8             return moddayNum;
  9         }
 10 
 11         //当天与该周星期日相差的天数
 12         public static int getSundayNum(System.DayOfWeek dw)
 13         {
 14             int weeknow = Convert.ToInt32(dw);
 15             int sundayNum = 7 - weeknow;
 16             return sundayNum;
 17         }
 18 
 19         //该周星期一的日期
 20         public static string getWeekMonday(System.DayOfWeek dw, string timeFormateStr)
 21         {
 22             int weeknow = Convert.ToInt32(dw);
 23             int moddayNum = (-1* weeknow + 1;
 24             string weekMonday = System.DateTime.Now.AddDays(moddayNum).Date.ToString(timeFormateStr);
 25             return weekMonday;
 26         }
 27 
 28         //该周星期日的日期
 29         public static string getWeekSunday(System.DayOfWeek dw, string timeFormateStr)
 30         {
 31             int weeknow = Convert.ToInt32(dw);
 32             int sundayNum = 7 - weeknow;
 33             string weekSunday = System.DateTime.Now.AddDays(sundayNum).Date.ToString(timeFormateStr);
 34             return weekSunday;
 35         }
 36 
 37         //本周是本年第几周
 38         public static int weekNum(System.DayOfWeek dw)
 39         {
 40             int weeknow = Convert.ToInt32(dw);//今天星期几
 41             int daydiff = (-1* (weeknow + 1);//今日与上周末的天数差
 42             int days = System.DateTime.Now.AddDays(daydiff).DayOfYear;//上周末是本年第几天
 43             int weeks = days / 7;
 44             if (days % 7 != 0)
 45             {
 46                 weeks++;
 47             }
 48             return (weeks + 2);
 49         }
 50 
 51         //获取某年的第一天
 52         public static string YearOneDay(DateTime argTime)
 53         {
 54             int yearDay = argTime.DayOfYear;
 55             TimeSpan ts = new TimeSpan(yearDay);
 56             argTime = argTime.Subtract(ts);
 57             return argTime.ToShortDateString();
 58         }
 59 
 60 
 61         /// <summary>
 62         /// 得到一年中的某周的起始日和截止日
 63         /// 年 nYear
 64         /// 周数 nNumWeek
 65         /// 周始 out dtWeekStart
 66         /// 周终 out dtWeekeEnd
 67         /// </summary>
 68         /// <param name="nYear"></param>
 69         /// <param name="nNumWeek"></param>
 70         /// <param name="dtWeekStart"></param>
 71         /// <param name="dtWeekeEnd"></param>
 72         public static void GetWeek(int nYear, int nNumWeek, out   DateTime dtWeekStart, out   DateTime dtWeekeEnd)
 73         {
 74             DateTime dt = new DateTime(nYear, 11);
 75             dt = dt + new TimeSpan((nNumWeek - 1* 7000);
 76             //dtWeekStart = dt.AddDays(-(int)dt.DayOfWeek + (int)DayOfWeek.Monday);
 77             //dtWeekeEnd = dt.AddDays((int)DayOfWeek.Saturday - (int)dt.DayOfWeek + 1);
 78 
 79             dtWeekStart = dt.AddDays(Convert.ToDouble((0 - Convert.ToInt16(dt.DayOfWeek))));
 80             dtWeekeEnd = dt.AddDays(Convert.ToDouble((6 - Convert.ToInt16(dt.DayOfWeek))));
 81         }
 82 
 83 
 84         /**/
 85         /// <summary>
 86         /// 求当前日期是一年的中第几周
 87         /// </summary>
 88         /// <param name="date"></param>
 89         /// <returns></returns>
 90         public static int WeekOfYear(DateTime curDay)
 91         {
 92             int firstdayofweek = Convert.ToInt32(Convert.ToDateTime(curDay.Year.ToString() + "" + "1-1 ").DayOfWeek);
 93 
 94             int days = curDay.DayOfYear;
 95             int daysOutOneWeek = days - (7 - firstdayofweek);
 96 
 97             if (daysOutOneWeek <= 0)
 98             {
 99                 return 1;
100             }
101             else
102             {
103                 int weeks = daysOutOneWeek / 7;
104                 if (daysOutOneWeek % 7 != 0)
105                     weeks++;
106                 return weeks + 1;
107             }
108         }
109 
110 
111         /// <summary>
112         /// 取得某月的第一天
113         /// </summary>
114         /// <param name="datetime">要取得月份第一天的时间 </param>
115         /// <returns> </returns>
116         public static DateTime FirstDayOfMonth(DateTime datetime)
117         {
118             return datetime.AddDays(1 - datetime.Day);
119         }
120 
121         /**/
122         /// <summary>
123         /// 取得某月的最后一天
124         /// </summary>
125         /// <param name="datetime">要取得月份最后一天的时间 </param>
126         /// <returns> </returns>
127         public static DateTime LastDayOfMonth(DateTime datetime)
128         {
129             return datetime.AddDays(1 - datetime.Day).AddMonths(1).AddDays(-1);
130         }
131 
132         /**/
133         /// <summary>
134         /// 取得上个月第一天
135         /// </summary>
136         /// <param name="datetime">要取得上个月第一天的当前时间 </param>
137         /// <returns> </returns>
138         public static DateTime FirstDayOfPreviousMonth(DateTime datetime)
139         {
140             return datetime.AddDays(1 - datetime.Day).AddMonths(-1);
141         }
142 
143         /**/
144         /// <summary>
145         /// 取得上个月的最后一天
146         /// </summary>
147         /// <param name="datetime">要取得上个月最后一天的当前时间 </param>
148         /// <returns> </returns>
149         public static DateTime LastDayOfPrdviousMonth(DateTime datetime)
150         {
151             return datetime.AddDays(1 - datetime.Day).AddDays(-1);
152         }
153 
154         public static string workZh(string argName)
155         {
156             string strName = string.Empty;
157             switch (argName)
158             {
159                 case "Saturday":
160                     strName = "星期六";
161                     break;
162                 case "Sunday":
163                     strName = "星期日";
164                     break;
165                 case "Friday":
166                     strName = "星期五";
167                     break;
168                 case "Thursday":
169                     strName = "星期四";
170                     break;
171                 case "Wednesday":
172                     strName = "星期三";
173                     break;
174                 case "Tuesday":
175                     strName = "星期二";
176                     break;
177                 case "Monday":
178                     strName = "星期一";
179                     break;
180             }
181             return strName;
182         }
183 
184     }

 

 

posted @ 2009-12-22 10:19  gonganruyi  阅读(449)  评论(0编辑  收藏  举报