I have encountered the following issue with asp calender.
Currently, I am able to select multiple dates within the range (which I have set) which is within 1 month. However I am unable to get the correct selected dates if my range is within 2 months (cross month).
Within 2 months:
If my calendar displays at the 1st month, I can still gets the dates from the 2 months but may have duplication dates on the 2nd months.
If my calendar display at the 2nd month, I am only able to get dates from the 2nd month.
Any advise for the above issue? Thanks in advance~ =)
protected void CalendarMain_DayRender(object sender, DayRenderEventArgs e)
{
DateTime minDate, maxDate;
if (!tbStartDate.Text.Equals("") && !tbEndDate.Text.Equals(""))
{
minDate = Convert.ToDateTime(tbStartDate.Text);
maxDate = Convert.ToDateTime(tbEndDate.Text);
if (e.Day.Date < minDate || e.Day.Date > maxDate)
{
e.Day.IsSelectable = false;
}
if (e.Day.Date >= minDate && e.Day.Date.Date <= maxDate)
{
e.Cell.BackColor = Color.FromName("#3f97ab");
}
}
DataTable dtgv = Session["dtSelectedDateData"] as DataTable;
if (dtgv != null)
{
foreach (DataRow drr in dtgv.Rows)
{
string DateValue = drr["Time_Start"].ToString();
if (e.Day.Date.ToString("dd/MM/yyyy") == DateValue.Substring(0, 10))
{
e.Cell.ForeColor = System.Drawing.Color.Pink;
e.Day.IsSelectable = false;
}
}
}
if (e.Day.IsSelected == true)
{
PatientsSchedule.listDatetime.Add(e.Day.Date);
}
Session["SelectedDate"] = PatientsSchedule.listDatetime;
}
public static List<DateTime> listDatetime = new List<DateTime>();
I have solved my issues. Here's the trick, I added a new List<> in my class file and a conditions statement under the calender SelectionChanged. So it will capture value for every click.
Related
This question already has answers here:
Linq Overlapped date range checking in single collection
(2 answers)
Closed 1 year ago.
I have a datatable having startdate and enddate columns. I want to check overlapping startdate and enddate using linq.
method input Parameters:
Input DataTable
startDate
endDate
01/Jan/2021
31/Jan/2021
01/Feb/2021
28/Feb/2021
Input Parameters
FromDate: 15/Feb/2021
ToDate: 20/Feb/2021
Expected OutPut: true
I have created a function to check for overlapping dates.
private bool IsDateOverlap(DateTime? FromDate, DateTime? ToDate, DataTable Table)
{
bool isOverlap = false;
try
{
for (int index = 0; index < Table.Rows.Count; index++)
{
if (index == this.RowID)
continue;
DateTime? rowFromDate = Convert.ToDateTime(Table.Rows[index]["startDate"]);
DateTime? rowToDate = Convert.ToDateTime(Table.Rows[index]["endDate"]);
isOverlap = (FromDate <= rowToDate && rowFromDate <= ToDate);
if (isOverlap)
break;
}
}
catch (Exception ex)
{ }
return isOverlap;
}
Its working fine.
I want to do it using linq. Thanks in Advance.
I have an approach that might help you how ever i use a custom object rather than a datatable.
public static List<inputDataTable> ListOfDates = new List<inputDataTable>
{
new inputDataTable { startDate = DateTime.Parse("01/Jan/2021"), endDate = DateTime.Parse("31/Jan/2021"),},
new inputDataTable { startDate = DateTime.Parse("01/Feb/2021"), endDate = DateTime.Parse("28/Feb/2021"),},
};
public static bool checkOverlapping(DateTime starDate, DateTime endDate, List<inputDataTable> storeDate)
{
var e = storeDate.Where(a=> (starDate >= a.startDate && starDate <= a.endDate) || (endDate >= a.startDate && starDate <= a.endDate) );
if(e.Count() > 0)
{
return true;
}
else
{
return false;
}
}
Also you can visit these threads;
Check dates fall within range using Linq
Check One Date is falling Between Date Range-Linq query
This question might sound very vague but I do not know how to phrase it in a better fashion.
I've been working on a month calendar that checks if the 2 selected dates are correct. There is 1 calender that uses to following code to figure out who called the event (The startdate textbox or enddate textbox)
MaskedTextBox b = (MaskedTextBox)sender;
currentSelectedDateBox = b.Name;
The startdate must be sooner than the enddate, both startdate and enddate can't be a date that already has a reservation(it shows a reservation by making the date bold), and the selected period can not have a reservation in it.
This is all working fine, although I have one problem: When I open the calendar and click on today, it closes, meaning the user has selected a date, although no date shows up and when I try to print the selected date: it won't print anything. If I select a different date, though, it will print that date, meaning that all dates can be selected except today.
If I click on a different day and then select today, it will work. This is really weird and I'm stuck.
Here's my code:
private void monthCalendar_DateChanged(object sender, DateRangeEventArgs e)
{
ErrorStartDateLabel.Visible = false;
ErrorEndDateLabel.Visible = false;
startdateLabel.ForeColor = Color.Black;
enddateLabel.ForeColor = Color.Black;
string day = monthCalendar.SelectionStart.Day.ToString();
string month = monthCalendar.SelectionStart.Month.ToString();
if (day.Length == 1) //part of the stringbuilder
{
day = "0" + day;
}
if (month.Length == 1) //part of the stringbuilder
{
month = "0" + month;
}
string date = day + "-" + month + "-" + monthCalendar.SelectionStart.Year.ToString(); //Date selected
if (startdateTextbox.Name == currentSelectedDateBox) //If the StartDate Calendar has been selected
{
startdateTextbox.Text = date;
startdate = monthCalendar.SelectionStart.Date;
}
else if (enddateTextbox.Name == currentSelectedDateBox)//If the EndDate Calendar has been selected
{
enddateTextbox.Text = date;
enddate = monthCalendar.SelectionStart.Date;
secondDateChecked = true;
}
if (secondDateChecked) //if the enddate textbox has data
{
if (enddate < startdate) //if enddate is smaller than the startdate
{
enddateTextbox.Text = "";
ErrorEndDateLabel.Text = "Uw gekozen huurperiode klopt niet!";
ErrorEndDateLabel.ForeColor = Color.Red;
enddateLabel.ForeColor = Color.Red;
ErrorEndDateLabel.Visible = true;
}
}
if (carHasReservation == true) //If there is a reservation
{
foreach (var bolddate in bolddates) //loop through all bolded dates
{
if (startdate == bolddate || enddate == bolddate)//if the startdate OR enddate is a bolded date
{
if (startdate == bolddate)
{
startdateTextbox.Text = "";
ErrorStartDateLabel.Text = "Deze startdatum is al gereserveerd!";
ErrorStartDateLabel.ForeColor = Color.Red;
ErrorStartDateLabel.Visible = true;
}
else
{
enddateTextbox.Text = "";
ErrorEndDateLabel.Text = "Deze einddatum is al gereserveerd!";
ErrorEndDateLabel.ForeColor = Color.Red;
ErrorEndDateLabel.Visible = true;
}
}
if (startdate <= enddate)
{
TimeSpan tisp = enddate - startdate;
int dateDiffer = tisp.Days;
for (int i = 0; i <= dateDiffer; i++) //Count the amount of days between the startdate and the enddate. For every day, check if one of those days is a bolded(reservation) date
{
if (startdate.AddDays(i) == bolddate)
{
reservationCollision = true;
}
}
}
}
}
else
{
//No reservation, do nothing!
}
if (reservationCollision) //if there is a boldeddate between the selected dates
{
ErrorStartDateLabel.Text = "Tijdens uw geselecteerde periode";
ErrorEndDateLabel.Text = " is er al een reservering geplaatst!";
ErrorStartDateLabel.ForeColor = Color.Red;
ErrorEndDateLabel.ForeColor = Color.Red;
startdateLabel.ForeColor = Color.Red;
enddateLabel.ForeColor = Color.Red;
ErrorStartDateLabel.Visible = true;
ErrorEndDateLabel.Visible = true;
enddateTextbox.Text = "";
reservationCollision = false;
}
}
My explanation and code might be a bit vague, I'm sorry for that. I thought it would be better to add more information than necessary instead of giving a bad explanation of my problem and code.
Using calendar control in Visual Studio for the web 2012, I am able to take dates from the SQL Server 2012 database (i.e to date and from date and highlight this dates in the calendar) I am also able to highlight the dates in-between the to date and from date.
All in all in my calendar at the moment I have dates 02/10/2013 (to date) and 04/10/2013 (from date) highlighted in the calendar and the dates in-between these dates. And also 15/10/2013 (to date) and 19/10/2013 (from date) highlighted and the dates in-between these dates are highlighted.
However I want to be able to randomly change the back colour of each of the selected date blocks in the calendar? How do I do this?
Many thanks
here is bit of the code that highlights the dates with a back color and makes them selectable and such. This code works perfectly fine but I want to be able to do the above?
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
if (dsHolidays != null)
{
foreach (DataRow dr in dsHolidays.Tables[0].Rows)
{
DateTime nextDate;
DateTime endDate;
nextDate = (DateTime)dr["date"];
endDate = (DateTime)dr["date1"];
if (nextDate <= e.Day.Date && endDate >= e.Day.Date)
{
e.Cell.BackColor = System.Drawing.Color.Gray;
// dates are unselectable
e.Day.IsSelectable = false;
}
}
}
// makes the all the first dates selectable
foreach (DataRow dr in dsHolidays.Tables[0].Rows)
{
DateTime nextDate1;
nextDate1 = (DateTime)dr["date"];
{
if (e.Day.Date == nextDate1)
{
e.Day.IsSelectable = true;
e.Cell.ForeColor = System.Drawing.Color.Blue;
}
}
}
}
Maybe this code can help
Random randomGen = new Random();
KnownColor[] names = (KnownColor[])Enum.GetValues(typeof(KnownColor));
Color newColor = Color.FromKnownColor(names[randomGen.Next(names.Length)]);
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
if (e.Day.IsWeekend)
{
e.Day.IsSelectable = false;
e.Cell.BackColor = System.Drawing.Color.Yellow;
}
if(e.Day.Date.Day%2==0 && !e.Day.IsOtherMonth && !e.Day.IsWeekend)
{
e.Day.IsSelectable = false;
e.Cell.BackColor = System.Drawing.Color.Orange;
e.Cell.ToolTip = "Booked";
}
if (e.Day.Date.Day % 2 != 0 && !e.Day.IsOtherMonth && !e.Day.IsWeekend)
{
e.Day.IsSelectable = false;
e.Cell.BackColor = System.Drawing.Color.PaleGreen;
e.Cell.ToolTip = "Available";
}
if(e.Day.Date.Day%5==0 && !e.Day.IsOtherMonth && !e.Day.IsWeekend )
{
e.Day.IsSelectable = false;
e.Cell.BackColor = System.Drawing.Color.Green;
e.Cell.ToolTip = "Fast Booking";
}
}
I'm very new to using this site and to C# .Net so be gentle!
What is my project? I am creating a timesheet for my parents side business that doctors can fill out online. There are call hours and regular hours that I have created if statements for to equate the correct hours for both call and regular.
What am I trying to accomplish? I would like the user to click one submit button and have each group of drop down lists corresponding to each day run through the equations and give a total.
What is my question? How can I first off - group a set of Drop Down Lists and then - how would the code look to say something like "for some label(LabelMondayCALL.Text), use this group (DDL_In_Monday, DDL_OutBreak_Monday, etc.) of drop downs to find what the label would be."
Why do I want to do this? To avoid copy and pasting pages of code for each individual day and to try and keep things clean and simple for possible future changes.
Here is some code:
DateTime MondayDDL4 = DateTime.Parse(DDL_Out_Monday.SelectedValue);
DateTime MondayDDL3 = DateTime.Parse(DDL_InBreak_Monday.SelectedValue);
DateTime MondayDDL2 = DateTime.Parse(DDL_OutBreak_Monday.SelectedValue);
DateTime MondayDDL1 = DateTime.Parse(DDL_In_Monday.SelectedValue);
else if ((MondayDDL1 <= FromCompHours) & (MondayDDL4 <= FromCompHours)) //comp time earlier than 6:30
{
LabelMondayREG.Text = "00:00:00";//works
LabelMondayCALL.Text = MondayDDL4.Subtract(MondayDDL1).ToString();//works
if ((BreakStart != "00:00:00") & (BreakEnd != "00:00:00"))
{
LabelMondayREG.Text = "00:00:00";
String CompTimeBreakHours = (BreakEndDT.Subtract(BreakStartDT)).ToString();
LabelMondayCALL.Text = ((DateTime.Parse(LabelMondayCALL.Text)) - (DateTime.Parse(CompTimeBreakHours))).ToString();
}
}
Thanks for any help you can provide and please let me know if you see anything else that I could simplify, like I said I'm fairly new to this stuff.
Here is some more code and a picture of the actual site, but here is a little bit more of a description of what I'm doing: Basically these equations are deciding what time (Call hours or Regular Hours) the doctors break should be subtracted from, for one day of the week, from a row of drop down menus. However I would prefer not to copy this code for each set of drop down lists. So I wanted to know if there was a way to create an instance to have the call hours and regular hours labels use only the drop downs that correspond to the day they are labeled.
so MondayDDL1, 2, ,3 and 4 run through the equation and their answers fill in LabelMondayCall and LabelMondayReg
and then
TuesdayDDL1, 2, ,3 and 4 run through the equation and their answers fill in LabelTuesdayCall and LabelTuesdayReg
Never mind no image I don't have enough reputation
here is a bit of what the layout looks like though
mondayDDL1 mondayDDL2 mondayDDL3 mondayDDL4 LABELmondayREG LABELmondayCALL
tuesdayDDL1 tuesdayDDL2 tuesdayDDL3 tuesdayDDL4 LABELtuesdayREG LABELtuesdayCALL
protected void ButtonCalculate_Click(object sender, EventArgs e)
{
//DropDownList2.Items.Clear();
//DropDownList2.SelectedValue = null;
//DropDownList3.Items.Clear();
//DropDownList3.SelectedValue = null;
if (DDL_OutBreak_Monday.SelectedValue == "----")
{
DDL_OutBreak_Monday.SelectedValue = DateTime.Parse("00:00:00").ToShortTimeString();
}
if (DDL_InBreak_Monday.SelectedValue == "----")
{
DDL_InBreak_Monday.SelectedValue = DateTime.Parse("00:00:00").ToShortTimeString();
}
DateTime MondayDDL4 = DateTime.Parse(DDL_Out_Monday.SelectedValue);
DateTime MondayDDL3 = DateTime.Parse(DDL_InBreak_Monday.SelectedValue);
DateTime MondayDDL2 = DateTime.Parse(DDL_OutBreak_Monday.SelectedValue);
DateTime MondayDDL1 = DateTime.Parse(DDL_In_Monday.SelectedValue);
//DDL1 = DateTime.Parse(DDL_In_Tuesday.SelectedValue);// END POINT---------------------------END POINT
String BreakStart = DDL_OutBreak_Monday.SelectedValue;
String BreakEnd = DDL_InBreak_Monday.SelectedValue;
DateTime BreakStartDT = DateTime.Parse(BreakStart);
DateTime BreakEndDT = DateTime.Parse(BreakEnd);
DateTime FromCompHours = DateTime.Parse("6:30:00");
DateTime ToCompHours = DateTime.Parse("16:30:00");
Label1.Text = "";
Label2.Text = "";
Label3.Text = "";
LabelMondayREG.Text = "";
LabelMondayCALL.Text = "";
//int result = DateTime.Compare(DDL1, DDL2);
if ((MondayDDL1 <= FromCompHours) & (MondayDDL4 >= ToCompHours))//Comp time at both ends
{
Label2.Text = FromCompHours.Subtract(MondayDDL1).ToString(); //finds comp hours before 6:30 WORKS
Label3.Text = MondayDDL4.Subtract(ToCompHours).ToString(); //finds comp hours after 16:30 WORKS
LabelMondayREG.Text = ("10:00:00");
//LabelHolder.Text = (DateTime.Parse(Label2.Text)) + (DateTime.Parse(Label3.Text)).ToString(); //adds the two comp hours together
//TimeSpan SubtractReg = DDL2.Subtract(DDL1); //finds the difference of from minus to
DateTime To = DateTime.Parse(Label2.Text);//convert text to datetime of earlier time
DateTime From = DateTime.Parse(Label3.Text);//convert text to datetime of later time
LabelMondayCALL.Text = (To.TimeOfDay + From.TimeOfDay).ToString();
//LabelMondayCALL.Text = "10:00:00";
if ((BreakStartDT != DateTime.Parse("00:00:00")) & (BreakEndDT != DateTime.Parse("00:00:00")))
{
//DateTime MondayBreak = MondayDDL3.Subtract(MondayDDL2);
if ((MondayDDL2 <= FromCompHours) & (MondayDDL3 <= FromCompHours)) //Start before 6:30 end after 16:30 w/ break before 6:30
{
LabelMondayCALL.Text = TimeSpan.Parse(LabelMondayCALL.Text).Subtract(MondayDDL3.Subtract(MondayDDL2)).ToString();
//LabelMondayCALL.Text = "error1"; //(DateTime.Parse(LabelMondayCALL.Text)).ToString();
}
if ((ToCompHours >= MondayDDL2) & (MondayDDL2 >= FromCompHours) & (ToCompHours >= MondayDDL3) & (MondayDDL3 >= FromCompHours)) //Start before 6:30 end after 16:30 /w break between 6:30 and 16:30
{
LabelMondayREG.Text = TimeSpan.Parse(LabelMondayREG.Text).Subtract(MondayDDL3.Subtract(MondayDDL2)).ToString();
}
if ((MondayDDL2 >= ToCompHours) & (MondayDDL3 >= ToCompHours)) //Start before 6:30 end after 16:30 /w break after 16:30
{
LabelMondayCALL.Text = TimeSpan.Parse(LabelMondayCALL.Text).Subtract(MondayDDL3.Subtract(MondayDDL2)).ToString();
}
if ((MondayDDL2 <= FromCompHours) & (MondayDDL3 >= FromCompHours) & (MondayDDL3 <= ToCompHours))
{
LabelMondayCALL.Text = TimeSpan.Parse(LabelMondayCALL.Text).Subtract(FromCompHours.Subtract(MondayDDL2)).ToString();
LabelMondayREG.Text = TimeSpan.Parse(LabelMondayREG.Text).Subtract(MondayDDL3.Subtract(FromCompHours)).ToString();
}
if ((MondayDDL2 <= ToCompHours) & (MondayDDL2 >= FromCompHours) & (MondayDDL3 >= ToCompHours))
{
LabelMondayCALL.Text = TimeSpan.Parse(LabelMondayCALL.Text).Subtract(ToCompHours.Subtract(MondayDDL2)).ToString();
LabelMondayREG.Text = TimeSpan.Parse(LabelMondayREG.Text).Subtract(MondayDDL3.Subtract(ToCompHours)).ToString();
}
}
In my program there are two datetimepickers. I need to get the days in between the chosen dates and store them into a list or array. All these are to be done in the second datetimepicker value changed event
private void dateTimePickertodate_ValueChanged(object sender, EventArgs e)
{
if (dateTimePickertodate.Value <=dateTimePickerfromdate.Value)
{
MessageBox.Show("Choose Correct date");
textBoxnumofdays.Clear();
}
else
{
cleave = new LeaveApplication(constr);
TimeSpan span = dateTimePickertodate.Value - dateTimePickerfromdate.Value;
if (Mode == 1)
{
textBoxnumofdays.Text = Convert.ToString(span.Days + 2);
}
else
{
textBoxnumofdays.Text = Convert.ToString(span.Days + 1);
}
}
}
You may try something on these lines
DateTime dtFrom = new DateTime(2011, 02, 5);
DateTime dtTo = new DateTime(2011, 02, 9);
List<DayOfWeek> days = new List<DayOfWeek>();
while (dtTo != dtFrom)
{
dtFrom = dtFrom.AddDays(1);
days.Add(dtFrom.DayOfWeek);
}
days would have your week days list (if this is what you intent to have)
You could try this to get all specified weekdays between two dates
public List<DateTime> GetSelectedDaysInPeriod(DateTime startDate, DateTime endDate, List<DayOfWeek> daysToCheck)
{
var selectedDates = new List<DateTime>();
if (startDate >= endDate)
return selectedDates; //No days to return
if (daysToCheck == null || daysToCheck.Count == 0)
return selectedDates; //No days to select
try
{
//Get the total number of days between the two dates
var totalDays = (int)endDate.Subtract(startDate).TotalDays;
//So.. we're creating a list of all dates between the two dates:
var allDatesQry = from d in Enumerable.Range(1, totalDays)
select new DateTime(
startDate.AddDays(d).Year,
startDate.AddDays(d).Month,
startDate.AddDays(d).Day);
//And extracting those weekdays we explicitly wanted to return
var selectedDatesQry = from d in allDatesQry
where daysToCheck.Contains(d.DayOfWeek)
select d;
//Copying the IEnumerable to a List
selectedDates = selectedDatesQry.ToList();
}
catch (Exception ex)
{
//Log error
//...
//And re-throw
throw;
}
return selectedDates;
}
This question has already been answered here:
http://bytes.com/topic/net/answers/48324-their-method-timespan-time-function
Also, you want to use the TotalDays property of TimeSpan and not just the day.
use TimeSpan.TotalDays
the difference of 2 DateTimes is TimeSpan, which has property TotalDays, the count of days between 2 datetimes