Reputation: 1803
I'm currently working on a project which needs to check for the day and time, then compare these with values in my database. This needs to be done for an Live Chat, while we have opened. I'm new to ASP.Net and this is giving me an headache.
I Have a table which contains these Rows: - weekday - starttime - endtime
In my Controller for the Chat I have tried this so far
private Model1Container db = new Model1Container();
public ActionResult Index(Openinghours openinghours)
{
ViewBag.Opening = db.OpeninghoursSatz.ToList();
string CurrentDay = DateTime.Now.DayOfWeek.ToString();
string CurrentTime = DateTime.Now.TimeOfDay.ToString();
bool IsWeekDay= db.OpeninghoursSatz.Where(i => i.weekday == CurrentDay).Any();
var Starttime = db.OpeninghoursSatz.Find(CurrentDay);
Debug.Write("!!++" + Starttime);
My exact Problem is to compare the time with the two rows from my table. I thought about putting the values in a variable and then compare them, but there must be a cleaner way for this.
I hope I was able to point out what my problem is, if not please ask.
With regards,
Nico
EDIT: Maybe I should say that the database holds the hour it opens and the hour it closes (e.g. 9 and 17). I need to check if the current time is between these two.
Upvotes: 0
Views: 1644
Reputation: 1346
int getStartHour = DateTime.Now.Hour;
int weekDay= (int)DateTime.Now.DayOfWeek;
bool value = db.OpeninghoursSatz.Where(m => getStartHour >= m.Starttime && getStartHour <= m.EndTime && m.weekday == weekDay).Any()
/// if everything is in int. or if in string
string getStartHour = DateTime.Now.Hour.ToString();
string weekDay= DateTime.Now.DayOfWeek.ToString();
bool value = db.OpeninghoursSatz.Where(m => getStartHour >= m.Starttime && getStartHour <= m.EndTime && m.weekday == weekDay).Any();
But in this case for Thursday , we will get Thursday, but in Int we will get 4.
Upvotes: 1
Reputation: 27944
The line looks odd to me:
var Starttime = db.OpeninghoursSatz.Find(CurrentDay);
I guess you should change it to something like:
if (IsWeekDay){
var Starttime = db.OpeninghoursSatz.Where(i => i.weekday == CurrentDay).First().StartTime;
Debug.Write("!!++" + Starttime);
}
Upvotes: 0