how to create range datetime for data without switch in c#? - c#

i get onlinedata,i want show in cells base Specified time,datetime range is 9:00:00 to 13:00:00 and i want each 5 minute get data from onlinedata.i dont use switch or if-else
List<TradeDto> cMIMData = new List<TradeDto>();
cMIMData = (List<TradeDto>)data;
cMIMData have TradeDate,TradeDateTime,TradeTime,...,and value properties

your description was not good enough, must use while base on date by increase 5 min, you can use loop(while,for,...) and check online data with lambda expression
cMIMData = cMIMData.OrderByDescending(x => x.TradeDateTime).ToList();
startDate = new DateTime(currentDate.Year, currentDate.Month, currentDate.Day, 9, 5, 0);
endDate = new DateTime(currentDate.Year, currentDate.Month, currentDate.Day, 13, 0, 0);
while (startDate < endDate)
{
var value = cMIMData.Where(x => x.TradeDateTime.Date == your date && x.TradeTime >= startDate.AddMinutes(-5).TimeOfDay &&
x.TradeTime <= startDate.TimeOfDay).First(); //get last item value in Limit specified time
startDate = startDate.AddMinutes(5);
}

Related

Finding out if specific date is somewhere between two other dates for any year in C#

So I'm making pretty simple console app for applying a discount for your flight depending on your case. In one case I can't add a discount if flight date is somewhere between YYYY/12/20 – YYYY(+1)/01/10 and between YYYY/03/20 – YYYY/04/10 but I'm not really sure how to approach that. I thought about specifying it like this:
DateTime springSeasonStart = new DateTime(DateTime.Now.Year, 03, 20);
DateTime springSeasonEnd = new DateTime(DateTime.Now.Year, 04, 10);
and then check if my flightDate is somewhere between springSeasonStart and springSeasonEnd. If it is, the discount wouldn't apply but it doesn't really work because year can't be specified, only month and day can. So what can I do to achieve what I need?
Check if the start month is greater than end month. If it's greater, than it's next year and you need to add 1 year for the end date.
var date = new DateTime(2021, 12, 25); // flight date
// season start
var monthStart = 12;
var dayStart = 20;
// season end
var monthEnd = 1;
var dayEnd = 10;
var yearEndModifier = monthStart > monthEnd ? 1 : 0; // +1 if next year
var SeasonStart = new DateTime(date.Year, monthStart, dayStart);
var SeasonEnd = new DateTime(date.Year + yearEndModifier, monthEnd, dayEnd);
if (date >= SeasonStart && date <= SeasonEnd)
Console.WriteLine("Discount");
else
Console.WriteLine("No Discount");
In second case YYYY/03/20 – YYYY/04/10, yearEndModifier = 0 because start month is lesser than end month.

iterating between dates, hours and minutes

I am trying to iterate between 2 dates received as inputs and print every 5 minutes (during working hours)
Seems like I am getting to an endless and can get my app stop at endTime
DateTime startDate = new DateTime(2018, 1, 1);
DateTime endDate = new DateTime(2018, 3, 1);
// day in month
for (DateTime date = startDate; date < endDate; date = date.AddDays(1))
{
if (date.DayOfWeek == DayOfWeek.Friday || date.DayOfWeek == DayOfWeek.Saturday)
continue;
//iterate every hour
for (var hour = date; hour < hour.AddDays(1); hour = hour.AddHours(1))
{
if (hour.Hour < 8 || hour.Hour > 17)
continue;
//iterate every minute
for (var min = date; min <= min.AddDays(1); min = min.AddMinutes(5))
{
Console.WriteLine(min);
}
}
}
Maybe you're overcomplicating; take a look into this:
var startDate = new DateTime(2018, 1, 1);
var endDate = new DateTime(2018, 3, 1);
while ((startDate = startDate.AddMinutes(5)) < endDate)
{
if (startDate.Hour < 8 || startDate.Hour > 17 ||
startDate.DayOfWeek == DayOfWeek.Saturday ||
startDate.DayOfWeek == DayOfWeek.Sunday)
continue;
Console.WriteLine("{0:ddd, MMM dd, yyyy HH:mm}", startDate);
}
You just need a loop, incrementing by 5 minutes until the endDate is met; inside the loop you skip all values you don't want (weekends and non-business hours).
In this code I'm reusing the startDate as work variable, but you definitely could create a new one and make things clearer.

Do not include 2nd date range in 1st date range

DateTime startDate1 = (DateTime)StartDate;
DateTime endDate1 = (DateTime)EndDate;
DayOfWeek? day = db.DayOfWeek;
var dayCount = dates.Count(x => x.DayOfWeek == day);
List<DateTime> dates =
Enumerable.Range(0, (int)((EndDate - StartDate).TotalDays) + 1)
.Select(n => StartDate.AddDays(n))
.ToList();
DateTime startDate1 = new DateTime(2018, 11, 01);
DateTime endDate1 = new DateTime(2018, 11, 30);
DateTime startDate2 = new DateTime(2018, 11, 25);
DateTime endDate2 = new DateTime(2018, 11, 30);
if ((startDate2 >= startDate1 && startDate2 <= endDate1) ||
(endDate2 >= startDate1 && endDate2 <= endDate1))
I want to count the number DayOfWeek inside of the range startDate1 and endDate1 and exclude startDate2 and endDate2 from that range completely.
In the example ranges above, my current code return '4' Mondays when I want it to return '3'.
Let's say the endDate1 is now (2018, 12, 31) and the rest of the dates are the same, it should return a count of '8' for Monday instead of the '9'.
Basically, I want to exclude the (2) range from the count regardless if it is within/overlaps the (1) range. How would I go about this?

Lambda between two dates explicit

I am in need of some wizards.
I have a table
Start End PersonID
-----------------------------------------------------
10/07/2017 00:00:00 18/07/2017 00:00:00 1
27/07/2017 00:00:00 27/07/2017 00:00:00 1
28/07/2017 00:00:00 28/07/2017 00:00:00 1
29/07/2017 00:00:00 29/07/2017 00:00:00 1
30/07/2017 00:00:00 30/07/2017 00:00:00 1
If I search for
Date Start = 11/07/2017
Date End = 12/07/2017
Using this query:
DateTime start = new DateTime(2017,07,11,0,0,0,0,0);
DateTime end = start.AddDays(1);
DateTime[] days = new DateTime[end.Subtract(start).Days];
for (int i = 0; i < end.Subtract(start).Days; i++)
{
var d = start.AddDays(i);
days[i] = d;
}
IQueryable block = tmOpen1.Calendar.Where(x => days.All(y => y >= x.start && y <= x.end)).Select(x => new { ID = x.PersonID });`
I get a positive result for ROW 1 (10/07/2017 - 18/07/2017)
However If I apply it against the remaining rows e.g. Filter
Date Start = 28/07/2017
Date End = 29/07/2017
Then obviously this will fail. How Can I get this side of the search to work.
E.g. Either
Take the first row and make it split out into individual rows
Make the Individual rows return true if a Person has several true conditions.
I hope one of the geniuses here can help.
Seems like all you really need is something like this:
DateTime start = new DateTime(2017,07,11,0,0,0,0,0);
DateTime end = start.AddDays(1);
var results = tmOpen1.Calendar
.Where(c => start <= c.end && end >= c.start)
.Select(x => new { ID = x.PersonID });
If your interval starts or ends somewhere between a start and end date from the table, than it means it is overlapping and you should included in your result.
tmOpen1.Calendar.Where(x => (startDate >= x.start && startDate <= x.end) || (endDate >= x.start && endDate <= x.end)).Select(x => new { ID = x.PersonID });
So an interval 10.07 - 27.07 should give you the first 2 rows, right?
Or is the interval supposed to be fully enclosed between 2 dates in the table?
From understanding of your question you want to know when the Date Start or Date End is within a range of dates.
You can check Date Start is within the date range or the Date End is within the date range
Example:
List<DateRange> dates = new List<DateRange>();
dates.Add(new DateRange()
{
StartDate = new DateTime(2017, 07, 10),
EndDate = new DateTime(2017, 07, 18)
});
dates.Add(new DateRange()
{
StartDate = new DateTime(2017, 07, 28),
EndDate = new DateTime(2017, 07, 28)
});
DateRange search1 = new DateRange()
{
StartDate = new DateTime(2017, 07, 11),
EndDate = new DateTime(2017, 07, 12)
};
DateRange search2 = new DateRange()
{
StartDate = new DateTime(2017, 07, 28),
EndDate = new DateTime(2017, 07, 29)
};
var result1 = dates.Where(x => search1.StartDate >= x.StartDate && search1.StartDate <= x.EndDate ||
search1.EndDate <= x.StartDate && search1.EndDate >= x.EndDate);
var result2 = dates.Where(x => search2.StartDate >= x.StartDate && search2.StartDate <= x.EndDate ||
search2.EndDate <= x.StartDate && search2.EndDate >= x.EndDate);
Simplier with the not valid time frame:
DateTime start = new DateTime(2017, 07, 11, 0, 0, 0, 0, 0);
DateTime end = start.AddDays(1);
var results = tmOpen1.Calendar.
.Where( c => ! ( c.Start > end || c.End < start) )
.Select(x => new { ID = x.PersonID } );
For DateTime start = new DateTime(2017, 07, 11, 0, 0, 0, 0, 0);
The result are:
TEST 1: 11/07/2017 00:00:00
Start:10/07/2017 00:00:00 End:18/07/2017 00:00:00 ID:1
For DateTime start = new DateTime(2017, 07, 28, 0, 0, 0, 0, 0);
The result are:
TEST 2: 28/07/2017 00:00:00
Start:28/07/2017 00:00:00 End:28/07/2017 00:00:00 ID:1
Start:29/07/2017 00:00:00 End:29/07/2017 00:00:00 ID:1
modelclassList= modelclassList.Where(x => x.gf_expdate>DateTime.Now).ToList();
to check expiry date and save back list of model class

How to find exact date range from list of date ranges by entering single date

I want to find the date range which falls in input date, following is structure
public class Duration
{
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
}
var durations = new List<Duration>();
var duration1 = new Duration()
{
StartDate = new DateTime(2017, 08, 1),
EndDate = new DateTime(2017, 08, 10)
};
durations.Add(duration1);
var duration2 = new Duration()
{
StartDate = new DateTime(2017, 08, 5),
EndDate = new DateTime(2017, 08, 10)
};
durations.Add(duration2);
var duration3 = new Duration()
{
StartDate = new DateTime(2017, 08, 5),
EndDate = new DateTime(2017, 08, 6)
};
durations.Add(duration3);
Now I want to find duration which is closest to the entered date for list of <Durations> with LINQ or for-loop
My expected result for currentDate=new DateTime(2017, 08, 7); is duration2
You first need to check if the currentDate is within the start and end dates of each range. For the ones that meet that condition, you calculate the "closeness" adding both distances. When you find one lapse(gap) smaller tan the previous, you save its index... and voilá
int lapse = Integer.MaxValue;
int counter = 0;
int index = 0;
foreach (d in durations) {
if (((d.StartDate <= currentDate) && (d.EndDate >= currentDate))) {
int newlapse = ((currentDate - d.StartDate).TotalDays + (d.EndDate - currentDate).TotalDays);
if ((newlapse < lapse)) {
lapse = newlapse;
index = counter;
}
}
counter +=1;
}
return durations(index);
If you need the middle of interval to be closest:
durations.OrderBy((d) => Math.Abs(d.EndDate.Ticks + d.StartDate.Ticks) / 2 - currentDate.Ticks).FirstOrDefault();
If you need the start of interval to be closest:
durations.OrderBy((d) => Math.Abs(d.EndDate.Ticks - currentDate.Ticks)).FirstOrDefault();
As D le mentioned above
First check if currentDate is within the start and end dates
Second select the duration with the minimal difference between start end end date
I used a nuget package called morelinq which gives nice extensions methods like MinBy:
var result = (from d in durations
where (d.StartDate <= currentDate && d.EndDate >= currentDate)
select d).MinBy(d => d.EndDate - d.StartDate);

Categories