获取当周(当月)的起始结束时间

下雨的天空。。。雷声轰隆隆!!!吵着心慌慌。。。起来顺便来此一游。
之前用过的获取当周当月的起始结束时间。现在就把代码贴出来。别的不多说,一句话实用。
  1using System;
  2using System.Data;
  3using System.Configuration;
  4using System.Web;
  5using System.Web.Security;
  6using System.Web.UI;
  7using System.Web.UI.WebControls;
  8using System.Web.UI.WebControls.WebParts;
  9using System.Web.UI.HtmlControls;
 10
 11namespace VehicleManagement.Common
 12{
 13    public class DateTimeDealWith
 14    {
 15
 16
 17        public string GetWeekRange(string strDate)
 18        {
 19            try
 20            {
 21                //需要判断的时间
 22                DateTime dTime = Convert.ToDateTime(strDate);
 23                return GetWeekindexRange(dTime);
 24            }

 25            catch (Exception ex)
 26            {
 27                throw new Exception(ex.Message);
 28            }

 29        }

 30      //***********************获取当前周的起始与结束时间*****************************//
 31        public string GetWeekindexRange(DateTime dTime)
 32        {
 33            try
 34            {
 35                int index = (int)dTime.DayOfWeek;
 36
 37                index = index == 0 ? 7 : index;
 38
 39                //当前周的范围
 40                DateTime retStartDay = dTime.AddDays(-(index - 1));
 41                DateTime retEndDay = dTime.AddDays(7 - index);
 42
 43                return WeekRangeToString(retStartDay, retEndDay);
 44            }

 45            catch (Exception ex)
 46            {
 47                throw new Exception(ex.Message);
 48            }

 49
 50        }

 51        private string WeekRangeToString(DateTime weekRangeStart, DateTime weekRangeEnd)
 52        {
 53            string strWeekRangeStart = weekRangeStart.ToString("yyyy/MM/dd");
 54            string strWeekRangeend = weekRangeEnd.ToString("yyyy/MM/dd");
 55
 56            return strWeekRangeStart + ";" + strWeekRangeend;
 57
 58        }

 59
 60        public string TheWeekOfFirstDay(string Day)
 61        {
 62            string FirstDay = "";
 63            FirstDay= GetWeekRange(Day).Split(';')[0];
 64            return FirstDay;
 65        }

 66        public string TheWeekOfLastDay(string Day)
 67        {
 68           string LastDay = "";
 69           LastDay= GetWeekRange(Day).Split(';')[1];
 70           return LastDay;
 71        }

 72
 73        //***********************获取当前月的起始与结束时间*****************************//
 74        public string GetMonthRange(string strDate)
 75        {
 76            try
 77            {
 78                //需要判断的时间
 79                DateTime dTime = Convert.ToDateTime(strDate);
 80                DateTime start = new DateTime(dTime.Year, dTime.Month, 1);
 81                DateTime end = new DateTime(dTime.Year, dTime.Month, DateTime.DaysInMonth(dTime.Year, dTime.Month));
 82                return MonthRangeToString(start, end);
 83            }

 84            catch (Exception ex)
 85            {
 86                throw new Exception(ex.Message);
 87            }

 88        }

 89
 90        private string MonthRangeToString(DateTime MonthRangeStart, DateTime MonthRangeEnd)
 91        {
 92            string strMonthRangeStart = MonthRangeStart.ToString("yyyy/MM/dd");
 93            string strMonthRangeend = MonthRangeEnd.ToString("yyyy/MM/dd");
 94
 95            return strMonthRangeStart + ";" + strMonthRangeend;
 96
 97        }

 98
 99        public string TheMonthOfFirstDay(string Day)
100        {
101            string FirstDay = "";
102            FirstDay = GetMonthRange(Day).Split(';')[0];
103            return FirstDay;
104        }

105        public string TheMonthOfLastDay(string Day)
106        {
107            string LastDay = "";
108            LastDay = GetMonthRange(Day).Split(';')[1];
109            return LastDay;
110        }

111    }

112}

posted @ 2009-08-30 15:48  萍水相逢  阅读(470)  评论(0编辑  收藏  举报