我需要有关自动分配日期的帮助。我找不到正确的方法。 现在的问题是我有健身计划。我将添加新用户。当我添加时,我也会添加程序。健身计划有限制。
例如。 8个洞。然后用户选择每个星期六和星期日。
16.03.2019
,17.03.2019
,23.03.2019
,24.03.2019
,
30.03.2019
,31.03.2019
,06.04.2019
,07.04.2019
由于他的选择,日期将自动分配。 uye.DAYS选择星期几。例如'0,6'和uye.SURE表示限制范围
if(uy.UYELIK== "PLATES")
{
DateTime date = DateTime.Now.Date;
System.TimeSpan duration = new System.TimeSpan(1, 0, 0, 0);
for (int i = 0; i < uye.SURE; i++)
{
date = date.Add(duration);
var list = uye.DAYS.Split(',');
for(int j = 0; j < list.Length; j++)
{
if ((int)date.DayOfWeek == Convert.ToInt32(list[j]))
{
HR_FITNESS_USER_PLATES_PROGRAM program = new HR_FITNESS_USER_PLATES_PROGRAM();
program.REF_HOCA = uye.HOCA;program.SEANS_LIMIT = uye.SURE;program.SEANS_TIME = date;program.REF_UYELIK = uy.ID;program.SICIL = uye.SICIL.ToString();
db.HR_FITNESS_USER_PLATES_PROGRAM.Add(program);db.SaveChanges();
}
}
}
}
此代码不正确。如果不是其中之一,我无法增加一天的时间。我该怎么办?
预先感谢
答案 0 :(得分:0)
据我了解,您需要为会员插入周末健身计划。这是一个简单的伪代码,
var startDate = DateTime.Today; //today
int currentDayOfWeek = (int)startDate.DayOfWeek; //today
//saturday of this week, disregarding if today is
//saturday or sunday, it is up to you to enhance this.
DateTime thisSaturday = startDate.AddDays(6 - currentDayOfWeek);
var sessions = 8;
for (int i = 0; i<sessions; i++)
{
SaveToDb(thisSaturday);
thisSaturday = thisSaturday.AddDays(7); //next week's weekend
}
private void SaveToDb(DateTime saturday)
{
DateTime sunday = saturday.AddDays(1);
//insert data for saturday and sunday
}