效果展示:

需求说明:
1.时间段固定一小时一个时间段,也就是7:00-8:00是一个时间段,8:00-9:00是一个时间段
2.预约天数为 当前时间——14天后
3.用户预约了某服务人员及某天的某个时间段后,其他人不可预约该服务人员的该时间段

实现说明:
主要在于时间之间的转换(转为当天的分钟数),然后再判断该分钟数是否被预约了,当前时间是否为有效预约时间(如当前时间为13:00,就不能预约比13:00早的时间),设置状态值。

代码实现:
准备三个方法,前两个用来时间的转换,最后一个是数据JSON返回的方法

     
        #region 当天时间转为当天的总分钟数
        public double TodayTotalMin(DateTime dt)
        {

            TimeSpan Time = new TimeSpan(dt.Ticks);

            DateTime ZeroDateTime = Convert.ToDateTime(dt.ToString("yyyy-MM-dd") + " 00:00:00");
            TimeSpan TsZeroDateTime = new TimeSpan((ZeroDateTime.Ticks));
            return (Time - TsZeroDateTime).TotalMinutes;
        }
        #endregion

        #region 根据当日的总分钟数转24小时时间制
        public string TodayTime(double Minute)
        {
            DateTime ZeroDateTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd ") + " 00:00:00");
            return ZeroDateTime.AddMinutes(Minute).ToString("yyyy-MM-dd HH:mm");

        }
        #endregion

JSON数据返回方法

        
        public string resultData(int code, string Msg, object data, int totalcount)
        {
            ReturnBaseCMDT result = new ReturnBaseCMDT();
            result.code = code;
            result.data = code == 1 ? data : new Object();
            result.msg = Msg;
            result.totalcount = totalcount;
            string strResult = ToJson(result);
            //时间处理
            strResult = Regex.Replace(strResult, @"\\/Date\((\d+)\)\\/", match =>
            {
                DateTime dt = new DateTime(1970, 1, 1);
                dt = dt.AddMilliseconds(long.Parse(match.Groups[1].Value));
                dt = dt.ToLocalTime();
                return dt.ToString("yyyy-MM-dd HH:mm");
            });
            return strResult;
        }

我们先显示可预约的日期让用户选择

 public string BeauticianDay()
        {

            //生成14天内的时间段
            DateTime StartTime = DateTime.Now;

            List DayList = new List();
            string DayName = "";
            for (int i = 0; i < 14; i++)
            {
                if (i == 0)
                {
                    DayName = "今天";
                }
                else if (i == 1)
                {
                    DayName = "明天";
                }
                else if (i == 2)
                {
                    DayName = "后天";
                }
                else
                {
                    DayName = StartTime.AddDays(i).DayOfWeek.ToString();
                    switch (DayName)
                    {
                        case "Monday":
                            DayName = "周一";
                            break;
                        case "Tuesday":
                            DayName = "周二";
                            break;
                        case "Wednesday":
                            DayName = "周三";
                            break;
                        case "Thursday":
                            DayName = "周四";
                            break;
                        case "Friday":
                            DayName = "周五";
                            break;
                        case "Saturday":
                            DayName = "周六";
                            break;
                        case "Sunday":
                            DayName = "周日";
                            break;
                    }
                }

                DayList.Add(new BeauticianDayModel
                {
                    DateTime = StartTime.AddDays(i).ToString("yyyy-MM-dd"),
                    DayName = DayName
                });
            }

            return resultData(1, "succeed", DayList, 0);
        }

 public class BeauticianDayModel
    {
        public string DateTime { get; set; }
        public string DayName { get; set; }
    }

返回JSON:

{
    "code": 1,
    "msg": "succeed",
    "data": [
        {
            "DayName": "今天",
            "DateTime": "2017-08-24"
        },
        {
            "DayName": "明天",
            "DateTime": "2017-08-25"
        },
        {
            "DayName": "后天",
            "DateTime": "2017-08-26"
        },
        {
            "DayName": "周日",
            "DateTime": "2017-08-27"
        },
        {
            "DayName": "周一",
            "DateTime": "2017-08-28"
        },
        {
            "DayName": "周二",
            "DateTime": "2017-08-29"
        },
        {
            "DayName": "周三",
            "DateTime": "2017-08-30"
        },
        {
            "DayName": "周四",
            "DateTime": "2017-08-31"
        },
        {
            "DayName": "周五",
            "DateTime": "2017-09-01"
        },
        {
            "DayName": "周六",
            "DateTime": "2017-09-02"
        },
        {
            "DayName": "周日",
            "DateTime": "2017-09-03"
        },
        {
            "DayName": "周一",
            "DateTime": "2017-09-04"
        },
        {
            "DayName": "周二",
            "DateTime": "2017-09-05"
        },
        {
            "DayName": "周三",
            "DateTime": "2017-09-06"
        }
    ],
    "totalcount": 0
}

选择某天可预约的时间段

 public string BeauticianTime()
        {
            string Day = Request["Day"];//2017-8-25 所选的日期
            string BeauticianId = Request["BeauticianId"];//服务人员的Id
            string UserId = Request["UserId"];//当前用户Id

           
            string Today = DateTime.Now.ToString("yyyy-MM-dd ");
            string StartTime="7:00";//营业开始时间
            string EndTime="17:00";//营业结束时间

            DateTime StartTime = Convert.ToDateTime(Today + StartTime);
            DateTime EndTime = Convert.ToDateTime(Today + EndTime);

            double StartMin = TodayTotalMin(StartTime);
            double EndMin = TodayTotalMin(EndTime);

            List TimesList = new List();


            //获取所选日期的预约记录
            List Raise_Book_ReservationRecordList = Raise_Book_ReservationRecordService.GetList("datediff(day,[StartTime],'" + Day + "')=0 and DistributorId=" + StoreId);

            int Status = 1; //1可选 2不可选 3已选 
            double NowMin = TodayTotalMin(DateTime.Now);
            for (double i = StartMin; i < EndMin; i = i + 60)
            {
                if (NowMin>i)
                {
                    Status = 0;
                    if (Raise_Book_ReservationRecordList.Count(p => TodayTotalMin((DateTime)p.StartTime) == i) > 0)
                    {
                        Status = 2;
                        if (Raise_Book_ReservationRecordList.Count(p => p.UserId == UserId) > 0)
                        {
                            Status = 3;
                        }
                    }
                    
                }
                else
                {
                    if (Raise_Book_ReservationRecordList.Count(p => TodayTotalMin((DateTime)p.StartTime) == i) > 0)
                    {
                        Status = 2;
                        if (Raise_Book_ReservationRecordList.Count(p => p.UserId == UserId) > 0)
                        {
                            Status = 3;
                        }
                    }
                    else
                    {
                        Status = 1;
                    } 
                }


                TimesList.Add(new BeauticianTimesModel
                {
                    Status = Status,
                    StartTimes = TodayTime(i),
                    EndTimes = TodayTime(i + 60)
                });
            }

            //处理结束时间不是60的整数倍的时候,直接使用结束时间
            DateTime EndTime2 = Convert.ToDateTime(TimesList.Last().EndTimes);
            if (TodayTotalMin(EndTime2) > EndMin)
            {
                TimesList.Last().EndTimes = TodayTime(EndMin);
            }

            return resultData(1, "succeed", TimesList, 0);
        }


 public class BeauticianTimesModel
    {
        public string StartTimes { get; set; }
        public string EndTimes { get; set; }
        public int Status { get; set; }
    }

返回JSON:

{
    "code": 1,
    "msg": "succeed",
    "data": [
        {
            "StartTimes": "2017-08-29 07:00",
            "EndTimes": "2017-08-29 08:00",
            "Status": 1
        },
        {
            "StartTimes": "2017-08-29 08:00",
            "EndTimes": "2017-08-29 09:00",
            "Status": 1
        },
        {
            "StartTimes": "2017-08-29 09:00",
            "EndTimes": "2017-08-29 10:00",
            "Status": 1
        },
        {
            "StartTimes": "2017-08-29 10:00",
            "EndTimes": "2017-08-29 11:00",
            "Status": 1
        },
        {
            "StartTimes": "2017-08-29 11:00",
            "EndTimes": "2017-08-29 12:00",
            "Status": 1
        },
        {
            "StartTimes": "2017-08-29 12:00",
            "EndTimes": "2017-08-29 13:00",
            "Status": 1
        },
        {
            "StartTimes": "2017-08-29 13:00",
            "EndTimes": "2017-08-29 14:00",
            "Status": 1
        },
        {
            "StartTimes": "2017-08-29 14:00",
            "EndTimes": "2017-08-29 15:00",
            "Status": 1
        },
        {
            "StartTimes": "2017-08-29 15:00",
            "EndTimes": "2017-08-29 16:00",
            "Status": 1
        },
        {
            "StartTimes": "2017-08-29 16:00",
            "EndTimes": "2017-08-29 17:00",
            "Status": 1
        }
    ],
    "totalcount": 0
}

预约时间