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
Related
Continuing from here, I have a table named Attendancelogs which has all the sorted logs CHeckTypes In and Out accordingly. (Thanks to #StephenMuecke the previous question was pretty much resolved) However, what I am trying to achieve next is to make a pairList of all the In and Out in a particular period (StartDate and EndDate) and then calculate the total hours of the employee.
Different Cases, already been taken care of in the previous question
I have two method, one called getSingleDevicePairs(int EnrollNumber, DateTime StartDate, DateTime EndDate) which creates the pairList and the second method getTimeSpentEachDay(List<Pair> pairList) which calculates the total hours of each day.
Pair.cs
public class Pair {
public int id { get; set; }
public int RegisterationId { get; set; }
public int EmpID { get; set; }
public DateTime InnDateTime { get; set; }
public DateTime OutDateTime { get; set; }
}
public List<Pair> getSingleDevicePairs(int EnrollNumber, DateTime StartDate, DateTime EndDate) {
DateTime today = DateTime.Now;
List<Pair> pairList = new List<Pair>();
var logs = db.AttendanceLogs.Where(x => x.RegisterationId == EnrollNumber && x.Date >= StartDate &&
x.Date <= EndDate && x.isIgnore != true && (x.CheckType == "In" || x.CheckType == "Out")).Distinct().ToList();
int loopEnd = 0;
bool oddLogs = false;
if (logs.Count % 2 == 0) {
loopEnd = logs.Count;
} else {
loopEnd = logs.Count - 1;
oddLogs = true;
}
bool inn = true;
if (loopEnd > 1) {
Pair pair = new Pair();
for (int v = 0; v < loopEnd; v++) {
if (inn) {
pair.InnDateTime = logs[v].DateTime;
inn = false;
} else {
pair.OutDateTime = logs[v].DateTime;
inn = true;
pairList.Add(pair);
pair = new Pair();
}
}
}
Bogus bogus = new Bogus();
DateTime bogusDate = new DateTime();
if (oddLogs) {
bogus.MachineNum = logs[logs.Count - 1].DeviceID;
bogus.RegisterationId = logs[logs.Count - 1].RegisterationId;
bogus.DateTime = logs[logs.Count - 1].DateTime;
bogusDate = logs[logs.Count - 1].DateTime;
}
return pairList;
}
^I changed the above method with different approach, since the above approach would mess up with Case 1 shown in the link above.
public List<Pair> getSingleDevicePairs(int EnrollNumber, DateTime StartDate, DateTime EndDate) {
DateTime today = DateTime.Now;
List<Pair> pairList = new List<Pair>();
var logs = db.AttendanceLogs.Where(x => x.RegisterationId == EnrollNumber && x.Date >= StartDate &&
x.Date <= EndDate && x.isIgnore != true && (x.CheckType == "In" || x.CheckType == "Out")).Distinct().ToList();
bool isCheck = false;
Pair pair = new Pair();
DateTime previous = logs.FirstOrDefault().DateTime;
foreach (var log in logs) {
if (!isCheck) {
pair.InnDateTime = log.DateTime;
isCheck = true;
} else {
pair.OutDateTime = log.DateTime;
isCheck = false;
}
pairList.Add(pair);
pair = new Pair();
}
return pairList;
}
^This approach again will fail at Case 1 as it is sequentially adding In and Out in the pairList.
public List<DateAndTime> getTimeSpentEachDay(List<Pair> pairList) {
List<DateAndTime> list = new List<DateAndTime>();
if (pairList.Count > 0) {
for (int i = 0; i < pairList.Count; i++) {
TimeSpan span = TimeSpan.Zero;
// bool flag = false;
int result = -1;
do {
span = span + (pairList[i].OutDateTime - pairList[i].InnDateTime);
result = -1;
if (i < pairList.Count - 1) {
DateTime p = (DateTime)pairList[i].InnDateTime;
DateTime q = (DateTime)pairList[i + 1].InnDateTime;
result = DateTime.Compare(p.Date, q.Date);
}
if (result == 0) {
i++;
// flag = true;
}
} while (result == 0);
//if (i == pairList.Count - 1)
//{
// span = span + (pairList[i].OutDateTime - pairList[i].InnDateTime) ?? TimeSpan.Zero;
//}
DateAndTime dnt = new DateAndTime();
dnt.date = ((DateTime)pairList[i].InnDateTime).ToString("yyyy-MM-dd");
dnt.Time = span;
list.Add(dnt);
}
}
return list.ToList();
}
I am trying to get total hours for each pair as well as a way to take the odd In's together which I can display on the calendar.
Below is an image of the calendar view for an employee which displays only the paired hours, I even want to show a In that would indicate the employee that he either forgot to check out or a missing entry was made.
*Lets say he had a checkin for 9th April of around 08:00 PM that had no Check out on that day and also in the NightersLimit (12 AM - 7 AM) then there should be a single line displayed on the calendar which can only happen if I bring the bogus record for a day from previous method.
There is nothing wrong with the getTimeSpentEachDay(); just a little changes in the getSingleDevicePairs(); method, your first approach fails since it eliminates the last entry of the logs in case of an odd number and your second approach fails since it is not taking account of the sequence the logs are stored in the table.
public List<Pair> getSingleDevicePairs(int EnrollNumber, DateTime StartDate, DateTime EndDate, int? missingEntry)
{
var logs = db.AttendanceLogs.Where(x => x.RegisterationId == EnrollNumber &&
x.Date >= StartDate && x.Date <= EndDate && x.isIgnore != true
&& (x.CheckType == "In" || x.CheckType == "Out")).Distinct().ToList();
if (logs.Count > 0)
{
bool isCheck = false;
Pair pair = new Pair();
DateTime previous = logs.FirstOrDefault().DateTime;
foreach (var log in logs)
{
if (!isCheck)
{
if (log.CheckType == "In")
{
pair.InnDateTime = log.DateTime;
isCheck = true;
}
}
else
{
if (log.CheckType == "Out")
{
pair.OutDateTime = log.DateTime;
isCheck = false;
pairList.Add(pair);
pair = new Pair();
}
if (pair.OutDateTime == DateTime.MinValue)
{
pair.InnDateTime = log.DateTime;
}
}
}
}
return pairList;
}
This completes the pair only when there is an Out for an In.
I am working on this employee performance review application. I need to send out reminder emails when the user has left 3 and 1 business days before the end of the 15 business day period.
Here is how I plan on doing it:
I have the Last Modified Date for each Review which I should be able to use to find out 15 business days in future(call it deadLineToSubmit for our example). Once I got deadLineToSubmit I should check to see if currentDate + 3 days(date) == deadLineToSubmit Then add that review to my list which I will be using to send email.
I have a function called GetByDayPrior where I am sending values 3 and 1 to check if user have left 3 business days or 1. Below is the code:
public List<int> GetByDaysPrior(int daysPrior)
{
List<int> response = new List<int>();
using (DAL.HumanResourcesEntities context = new DAL.HumanResourcesEntities())
{
DateTime currentDate = DateTime.Now.Date;
var lastModifiedDate = context.Reviews
.Where(s => s.IsActive == false)
.Where(s => s.ReviewStatusId == (int)ReviewStatuses.EmployeeSignature)
.Select(v => v.ModifiedDate)
.ToList();
AddBusinessDays(lastModifiedDate, daysPrior);
var lastRuntime = context.ApplicationRuntimeVariables.Where(y => y.ParameterName == "RemindersSentDT").Select(x => x.ParameterValue).FirstOrDefault().Date;
DateTime deadLineToSubmitReview = currentDate.AddDays(daysPrior).Date;
if (lastRuntime.AddHours(24).Date <= currentDate) //should it be == current date ?
{
var reviewIDs = context.Reviews
.Where(s => s.ModifiedDate < deadLineToSubmitReview)
.Where(s => s.ReviewStatusId == (int)ReviewStatuses.EmployeeSignature) //Check to make sure reminders only go when Review Status is Employee Signature
.Where(s => s.IsActive == false)
.Select(v => v.ReviewId)
.ToList();
response = reviewIDs.ToList();
}
return response;
}
}
I have another function called AddBusinessDays should give me the deadLineToSubmit when I pass in LastModifiedDate and 15 days as parameter. But currently its not working because firstly I cannot find out how to pass List of DataTime as a paramater in this function. Also when I defined date parameter in AddBusinessDays as List now all the instances of date and DayOfWeek are complanining
"Error 32 Cannot implicitly convert type
'System.Collections.Generic.List' to
'System.DateTime'
Below is my AddBusinessDays function.
public static DateTime AddBusinessDays(List<DateTime> date, int days)
{
if (days < 0)
{
throw new ArgumentException("days cannot be negative", "days");
}
if (days == 0) return date;
if (date.DayOfWeek == DayOfWeek.Saturday)
{
date = date.AddDays(2);
days -= 1;
}
else if (date.DayOfWeek == DayOfWeek.Sunday)
{
date = date.AddDays(1);
days -= 1;
}
date = date.AddDays(days / 5 * 7);
int extraDays = days % 5;
if ((int)date.DayOfWeek + extraDays > 5)
{
extraDays += 2;
}
return date.AddDays(extraDays);
}
Here's what I need to know: How to send DateTime List as parameter in AddBusinessDays function. How to make AddBusinessDays function work because now we are passing list instead of single dateTime. Is my thinking logic correct to solve this issue ?
Thanks a Lot! :)
date is a list and not a single date.
You can make an operation on all items of the list by using Select
for example:
lastModifiedDate = AddBusinessDays(lastModifiedDate, daysPrior);
Step 1: Convert your function to receive a single date
private static DateTime AddBusinessDays(DateTime date, int days)
{
if (days < 0)
{
throw new ArgumentException("days cannot be negative", "days");
}
if (days == 0) return date;
if (date.DayOfWeek == DayOfWeek.Saturday)
{
date = date.AddDays(2);
days -= 1;
}
else if (date.DayOfWeek == DayOfWeek.Sunday)
{
date = date.AddDays(1);
days -= 1;
}
date = date.AddDays(days / 5 * 7);
int extraDays = days % 5;
if ((int)date.DayOfWeek + extraDays > 5)
{
extraDays += 2;
}
return date.AddDays(extraDays);
}
Step 2: Use the single date function for the list of dates
public static List<DateTime> AddBusinessDays(List<DateTime> date, int days)
{
return date.Select(d => AddBusinessDays(d, days)).ToList();
}
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.
I have a WebApplication that manages events into tables like an Agenda..
For each event I have a StartDate and EndDate.
In the Application I want to export the events into a graph, and I want to check if the event passes the range of the required dates.
For example:
Event 1: StartDate (9 September) EndDate (14 September)
I want to check if it passes (10 Sept - 16 Sept)
This is a photo of the events example Graph:
This is a code that checks for only one date:
public static bool Within(this DateTime current, DateTime startTime, DateTime endTime)
{
return startTime < currentTime < endTime;
}
Edit:
To clarify more, I want the function to return true if the event passes the 2 dates range,
even if it start before the range or ends after the rang it should return true anyway.
only return false if it does not pass the range.
public static bool Within(DateTime one, DateTime two,
DateTime lowBound, DateTime highBound)
{
return one >= lowBound && two <= highBound;
}
public static bool Within(this DateTime current, DateTime startTime, DateTime endTime)
{
return startTime < currentTime && currentTime < endTime;
}
And call it twice
lowDate.Within(eventStart, eventEnd) && heDate.Within(eventStart, eventEnd)
Got the answer this way:
public static bool Within(DateTime StartDate, DateTime EndDate, DateTime StartRange, DateTime EndRange)
{
if ((StartDate == EndDate) && (StartRange <= StartDate && StartDate <= EndRange))
return true;
else
return ((StartRange >= StartDate && StartRange <= EndDate) || (EndRange >= StartDate && EndRange <= EndDate));
}
I want to compare two dates. In pseudo-code:
If the dueDate > now or dueDate = now
Then Fine Amount = something.
Else Fine Amount = 0
I wrote below code:
DateTime dueDate = Convert.ToDateTime(Reader1[3].ToString());
DateTime now = DateTime.Now;
int result = DateTime.Compare(dueDate, now);
if ((result < 0) || (result == 1))
{
row["Fine_Amount"] = Convert.ToDouble(Reader1[4].ToString());
}
else
{
row["Fine_Amount"] = 0;
}
This code gives wrong value, when
dueDate = 23-12-2011 AM 12:00:00
now = 23-12-2011 PM 05:26:54
I want to Compare:
dueDate = 23-12-2011
now = 23-12-2011
How do I remove the time in that?.
Adding below code is given result. But its to lengthy code.: -
DateTime dueDate = Convert.ToDateTime(Reader1[3].ToString());
DateTime now = DateTime.Now;
if (dueDate.Year < now.Year)
{
row["Fine_Amount"] = Convert.ToDouble(Reader1[4].ToString());
}
else if (dueDate.Year > now.Year)
{
row["Fine_Amount"] = 0;
}
else if (dueDate.Year == now.Year)
{
if (dueDate.Month < now.Month)
{
row["Fine_Amount"] = Convert.ToDouble(Reader1[4].ToString();
}
else if(dueDate.Month > now.Month)
{
row["Fine_Amount"] = 0;
}
else if(dueDate.Month == now.Month)
{
if(dueDate.Day < now.Day)
{
row["Fine_Amount"] = Convert.ToDouble(Reader1[4].ToString();
}
else
{
row["Fine_Amount"] = 0;
}
}
}
Is there any way to short this code?.
Answer For this Question is
if (dueDate.Date >= now.Date)
{
row["Fine_Amount"] = 0;
}
else
{
row["Fine_Amount"] = Convert.ToDouble(Reader1[4].ToString());
}
This datetime.date is gives
dueDate = 23-12-2011 AM 12:00:00 to 23-12-2011 AM 12:00:00
now = 23-12-2011 PM 05:26:54 to 23-12-2011 AM 12:00:00
You can use DateTime.Now.Date
According to docs, Date property returns: A new object with the same date as this instance, and the time value set to 12:00:00 midnight (00:00:00).
The DateTime class in C# supports comparison by simply using <, >, and == operators. Do it like your above written pseudo code.
dueDate.Date >= DateTime.Now.Date
Although you could use the .Date property of DateTime it is generally a good practice to compare a certain date to a date range:
startDate <= someDate && someDate < endDate
You can use the DateTime.Date Property to gets the date component of a DateTime value:
if (dueDate.Date >= DateTime.Now.Date)
use the Date property of DateTime object for comparison