asp.net时间控制

if ((material == true) && (model.Value6.IndexOf("进店时间") != -1))
                    {
                        if (model.Value1.Length == 8)
                        {
                            string strTime = model.Value1;
                            DateTime curTime = DateTime.Now;
                            model.Value1 = model.Value1.Insert(4, "-");
                            model.Value1 = model.Value1.Insert(7, "-");
                            DateTime dt = Convert.ToDateTime(model.Value1);
                            model.Value1 = model.Value1.Replace("-", "");

                            if (dt.Date.CompareTo(curTime.Date) == -1)
                            {
                                TimeSpan ts = curTime.Date.Subtract(dt.Date);
                                int day = ts.Days;
                                DayOfWeek week = curTime.DayOfWeek;                               
                                if (!CheckTime(week, day))
                                {
                                    throw new Exception("DateTime 不能早于上周一!");
                                }
                            }                          

                            if (dt.Date.CompareTo(curTime.Date) == 1)
                            {
                                throw new Exception("DateTime 只能取今天以前!");
                            }
                        }
                        else
                        {
                            throw new Exception("DateTime 长度不够!");
                        }
                    }
                   
                    private bool CheckTime(DayOfWeek week, int day)
        {
            bool b = false;
            switch (week.ToString().ToLower())
            {
                case "monday":
                    if (day < 8)
                    {
                        b = true;
                    }
                    break;
                case "tuesday":
                    if (day < 9)
                    {
                        b = true;
                    }
                    break;
                case "wednesday":
                    if (day < 10)
                    {
                        b = true;
                    }
                    break;
                case "thursday":
                    if (day < 11)
                    {
                        b = true;
                    }
                    break;
                case "friday":
                    if (day < 12)
                    {
                        b = true;
                    }
                    break;
                case "saturday":
                    if (day < 13)
                    {
                        b = true;
                    }
                    break;
                case "sunday":
                    if (day < 14)
                    {
                        b = true;
                    }
                    break;
                default:
                    break;
            }
            return b;
        }
posted @ 2011-07-05 10:24  斌哈  阅读(260)  评论(0编辑  收藏  举报