how i can get dates ranges list between 2 dates
for example,
start date is 12 February
end date is 23 May
so, the result must be like
12-Feb to 28-Feb
1-Mar to 31-Mar
1-April to 30-April
1-May to 23-May
Let's suppose you are checking only one year, i.e. the current one (2018).
Of course you can make StartDate and EndDate parametric.
There's nothing special in the code below; I used "d-MMMM" as date format since you seemed to ask for the minimum number of days ("d" does that, i.e. selects 1 and not 01 for the first day of a month) and the "long" name of the month ("MMMM" does that, check this answer).
If instead you just want the abbreviation, i.e. "Feb" for "February", you can use "MMM" (check this other answer).
DateTime StartDate = new DateTime(2018, 2, 12);
DateTime EndDate = new DateTime(2018, 5, 23);
for (int i = 0; i <= EndDate.Month - StartDate.Month; i++)
{
DateTime FirstDay = new DateTime(2018, StartDate.Month + i, 1);
if (i == 0)
{
FirstDay = StartDate;
}
DateTime LastDay = new DateTime(2018, StartDate.Month + i, DateTime.DaysInMonth(2018, FirstDay.Month));
if (i == EndDate.Month - StartDate.Month)
{
LastDay = EndDate;
}
Console.WriteLine(FirstDay.ToString("d-MMMM") + " to " + LastDay.ToString("d-MMMM"));
}
Console.ReadLine();
You can also use the following code to make it "cross-year":
static void Main(string[] args)
{
PrintDateList(new DateTime(2018, 2, 12), new DateTime(2018, 5, 23));
PrintDateList(new DateTime(2018, 11, 12), new DateTime(2019, 2, 23));
Console.ReadLine();
}
private static void PrintDateList(DateTime StartDate, DateTime EndDate)
{
Console.WriteLine("");
int TotalMonths = EndDate.Month - StartDate.Month +
(EndDate.Year - StartDate.Year) * 12;
int CurrentYear = StartDate.Year;
int MonthsToSubtract = 0;
for (int i = 0; i <= TotalMonths; i++)
{
int CurrentMonth = StartDate.Month + i;
if (StartDate.Month + i > 12)
{
if ((StartDate.Month + i) % 12 == 1)
{
CurrentYear++;
Console.WriteLine(CurrentYear.ToString());
MonthsToSubtract = StartDate.Month + i - 1;
}
CurrentMonth = StartDate.Month + i - MonthsToSubtract;
}
DateTime FirstDay = new DateTime(CurrentYear, CurrentMonth, 1);
if (i == 0)
{
FirstDay = StartDate;
}
DateTime LastDay = new DateTime(CurrentYear, CurrentMonth, DateTime.DaysInMonth(CurrentYear, FirstDay.Month));
if (i == TotalMonths)
{
LastDay = EndDate;
}
Console.WriteLine(FirstDay.ToString("d-MMMM") + " to " + LastDay.ToString("d-MMMM"));
}
}
The output is
12-February to 28-February
1-March to 31-March
1-April to 30-April
1-May to 23-May
12-November to 30-November
1-December to 31-December
1-January to 31-January
1-February to 23-February
Since you asked, I replaced Console.WriteLine with a List:
private static void PrintDateList(DateTime StartDate, DateTime EndDate)
{
List<Tuple<DateTime>> EventsDatesRangeList = new List<Tuple<DateTime>>();
Console.WriteLine("");
int TotalMonths = EndDate.Month - StartDate.Month +
(EndDate.Year - StartDate.Year) * 12;
int CurrentYear = StartDate.Year;
int MonthsToSubtract = 0;
for (int i = 0; i <= TotalMonths; i++)
{
int CurrentMonth = StartDate.Month + i;
if (StartDate.Month + i > 12)
{
if ((StartDate.Month + i) % 12 == 1)
{
CurrentYear++;
Console.WriteLine(CurrentYear.ToString());
MonthsToSubtract = StartDate.Month + i - 1;
}
CurrentMonth = StartDate.Month + i - MonthsToSubtract;
}
DateTime FirstDay = new DateTime(CurrentYear, CurrentMonth, 1);
if (i == 0)
{
FirstDay = StartDate;
}
DateTime LastDay = new DateTime(CurrentYear, CurrentMonth, DateTime.DaysInMonth(CurrentYear, FirstDay.Month));
if (i == TotalMonths)
{
LastDay = EndDate;
}
Console.WriteLine(FirstDay.ToString("d-MMMM") + " to " + LastDay.ToString("d-MMMM"));
EventsDatesRangeList.Add(new Tuple<DateTime>(FirstDay));
EventsDatesRangeList.Add(new Tuple<DateTime>(LastDay));
}
}
Related
I have two DatePickers one for StartDate and one for EndDate. For example when the user select 5 days I get 5 indexes with the date.
Now my question is how can I fill up this list with a minimum of 7 indexes (the filled up indexes should be empty)?
var startDate = StartDate;
var endDate = EndDate;
List<DateTime> Days = new List<DateTime>();
for (DateTime counter = startDate; counter <= endDate; counter = counter.AddDays(1))
{
if (counter.DayOfWeek == counter.DayOfWeek)
{
Days.Add(counter);
}
}
Here is what I have so fare. In my case this are working but it looks very ugly.
I just fill up the list with DateTime.MinValue on till I have at least 7 indexes. I am sure there is a bether way, just dont know how.
var startDate = StartDate;
var endDate = EndDate;
var standart = DateTime.MinValue;
List<DateTime> Days = new List<DateTime>();
for (DateTime counter = startDate; counter <= endDate; counter = counter.AddDays(1))
{
if (counter.DayOfWeek == counter.DayOfWeek)
{
Days.Add(counter);
}
}
if (Days.Count < 7)
{
Days.Insert(Days.Count, standart);
}
if (Days.Count < 7)
{
Days.Insert(Days.Count, standart);
}
if (Days.Count < 7)
{
Days.Insert(Days.Count, standart);
}
if (Days.Count < 7)
{
Days.Insert(Days.Count, standart);
}
if (Days.Count < 7)
{
Days.Insert(Days.Count, standart);
}
if (Days.Count < 7)
{
Days.Insert(Days.Count, standart);
}
You can calculate the number of days that you need to add to your list in order to reach 7, and then use it in a for loop to add them:
var startDate = new DateTime(2020, 01, 01);
var endDate = new DateTime(2020, 01, 05);
var standart = DateTime.MinValue;
List<DateTime> Days = new List<DateTime>();
int RemainingDays = 6 + (int)startDate.Day - (int)endDate.Day;
for (DateTime counter = startDate; counter <= endDate; counter = counter.AddDays(1))
Days.Add(counter);
if (RemainingDays > 0)
for (int i = 0; i < RemainingDays; i++)
Days.Insert(Days.Count, standart);
Or if you don't want to go thru another loop you can do:
if (RemainingDays > 0)
Days.AddRange(Enumerable.Repeat(standart, RemainingDays).ToList());
Note:
I didn't get what is your point with this line counter.DayOfWeek == counter.DayOfWeek I think that it doesn't make any sens in this context.
I am trying to keep track of how many meals a person has given from two dates you can enter from DateTimePickers.
There are three meals a day between specific times between:
6-8AM Meal 1
12-2PM Meal 2
5-7PM Meal 3
If a person arrives after one of the specific times, they miss out on a meal, so for example if I arrive on 1 Jan 2018 3AM and leave on 2 Jan 2018 2PM I will only receive 5 meals, as I missed out on the last meal.
This is the code I have come up with, but unfortunately I struggle to work the code out if the time between the two dates are more than one day apart.
private void button1_Click(object sender, EventArgs e)
{
dateTimeExit.CustomFormat = "MM/dd/yyyy hh:mm:ss tt";
dateTimeArrive.CustomFormat = "MM/dd/yyyy hh:mm:ss tt";
DateTime timeArrived = dateTimeArrive.Value;
DateTime timeExit = dateTimeExit.Value;
int totalHoursArrived = timeArrived.Hour;
int totalHoursExit = timeExit.Hour;
int totalCounts = 0;
int countA = 0; //6-8 AM
int countB = 0; //12-2PM
int countC = 0; //5-7PM
int totalDaysLeft = (timeExit - timeArrived).Days; //determines the number of days between the two given dates.
if (totalDaysLeft == 1)
{
totalCounts = 3;
countA = 1;
countB = 1;
countC = 1;
}
else if (totalDaysLeft < 1) //difference between the two dates is less than a day.
{
totalCounts = totalDaysLeft * 3;
countA = totalDaysLeft;
countB = totalDaysLeft;
countC = totalDaysLeft;
if (timeExit.Day == timeArrived.Day) //if the dates are on the same day and "less than one day"
{
if (totalHoursArrived <= 8 && totalHoursExit >= 17) //if date is before or on 8AM and leaves on or after 5PM.
{
countA = 1;
countB = 1;
countC = 1;
}
else if (totalHoursArrived <= 8 && (totalHoursExit >= 12 && totalHoursExit < 17)) //if date is before or on 8AM and leaves before 5PM
{
countA = 1;
countB = 1;
}
else if (totalHoursArrived <= 8 && totalHoursExit < 12) //if date is before or on 8AM and leaves before 12PM
{
countA = 1;
}
else if ((totalHoursArrived <= 12 && totalHoursArrived > 8) && totalHoursExit >= 17) //if date is before or on 12PM and leaves on or after 5PM
{
countB = 1;
countC = 1;
}
else if ((totalHoursArrived <= 12 && totalHoursArrived > 8) && totalHoursExit < 17) //if date is before or on 12PM and leaves before 5PM
{
countB = 1;
}
else if (totalHoursArrived >= 17) //if date is after or on 5PM
{
countC = 1;
}
totalCounts = countA + countB + countC;
}
else //less than a day, but not on same day exit time.
{
if (totalHoursArrived <= 8) //if date is before or on 8AM.
{
totalCounts = 3;
countA = 1;
countB = 1;
countC = 1;
}
else if (totalHoursArrived >= 12 && totalHoursArrived < 17)// //if date is after or on 12PM and arrival time is less than 5PM
{
totalCounts = 2;
countB = 1;
countC = 1;
}
else if (totalHoursArrived >= 17) //if date is after or on 5PM
{
totalCounts = 1;
countC = 1;
}
if (totalHoursExit > 0) // exit time
{
if (totalHoursExit >= 17)
{
totalCounts += 3;
countA += 1;
countB += 1;
countC += 1;
}
else if (totalHoursExit >= 12 && totalHoursExit < 17)
{
totalCounts += 2;
countA += 1;
countB += 1;
}
else if (totalHoursExit >= 6 && totalHoursExit < 12)
{
totalCounts += 1;
countA += 1;
}
}
}
}
else //more than two days difference between the two dates.
{
**//the part I am struggling to figure out**
}
lblTotalCountA.Text = "Count A: " + countA;
lblTotalCountB.Text = "Count B: " + countB;
lblTotalCountC.Text = "Count C: " + countC;
lblTotalAmount.Text = "Total Counts: " + totalCounts;
}
I find your code quite difficult to mantain (if you would like to add a fourth meal in the future, it would be a nightmare to change your code), so I'm giving you a different approach, as well as answering your question.
First I would define a class like this:
public class DayMeals
{
private int[] entryTimes = new int[] { 6, 12, 17 };
private int[] exitTimes = new int[] { 8, 14, 19 };
private int[] mealCounts = new int[3];
private bool countHalfMeals = false;
public DayMeals(bool countHalfMeals)
{
this.countHalfMeals = countHalfMeals;
}
public void AddFullDay()
{
mealCounts[0]++;
mealCounts[1]++;
mealCounts[2]++;
}
public void CountMealsForADay(DateTime timeArrived, DateTime timeExit)
{
for (int i = 0; i < mealCounts.Length; i++)
{
int mealEntryTime = entryTimes[i];
int mealExitTime = exitTimes[i];
if (timeArrived.Hour <= mealEntryTime && timeExit.Hour >= mealExitTime)
mealCounts[i]++;
else if (countHalfMeals && timeExit.Hour > mealEntryTime && timeExit.Hour <= mealExitTime)
mealCounts[i]++;
}
}
public void PrintMealsCount()
{
for (int i = 0; i < mealCounts.Length; i++)
{
System.Console.WriteLine($"Meal #{i + 1} count = {mealCounts[i]}");
}
}
}
Then, I would simply instantiate the class and call the functions:
void Main(string[] args)
{
CalculateMeals(new DateTime(2019, 1, 1, 15, 12, 1), new DateTime(2019, 1, 2, 18, 0, 0));
}
public static void CalculateMeals(DateTime timeArrived, DateTime timeExit)
{
// Number of full days
int fullDaysNumber = (timeExit - timeArrived).Days;
DayMeals dayMeals = new DayMeals(true);
for (int i = 0; i <= fullDaysNumber; i++)
{
int hoursDiff = (int)(timeExit - timeArrived).TotalHours;
if (timeExit.Day > timeArrived.Day && hoursDiff > 24)
{
dayMeals.AddFullDay();
// A trick to make the cycle work the next time
// You can use a different variable if you want to keep timeArrived unchanged
timeArrived = timeArrived.AddDays(1);
}
else if (timeExit.Day < timeArrived.Day)
{
break;
}
else
{
if (timeArrived.Day != timeExit.Day)
{
dayMeals.CountMealsForADay(timeArrived, new DateTime(1,1,timeArrived.Day,23,59,59));
dayMeals.CountMealsForADay(new DateTime(1,1,timeExit.Day,0,0,1), timeExit);
}
else
{
dayMeals.CountMealsForADay(timeArrived, timeExit);
}
}
}
dayMeals.PrintMealsCount();
}
I tried this code and it seems to work as expected. Please review it and let me know if this is what you wanted to achieve.
NOTE: I know the usage of "AddDays(1)" is counterintuitive, because I am keeping the same hour of day 1 for a subsequent day. But, if you are not interested about the fact that a person entered at 11 on Monday rather than on Tuesday, the meals count is the same. Basically, I'm just traslating the entry time to the last day.
Here you go, without looping. I simplified further by only using standard c# objects. The trick is in counting full days and work with timespans.
public static void Main(string[] args)
{
DateTime timeArrived = new DateTime(2019, 1, 5, 13, 53, 0);
DateTime timeExit = new DateTime(2019, 1, 6, 8, 46, 0);
TimeSpan startMeal1 = new TimeSpan(6, 0, 0);
TimeSpan endMeal1 = new TimeSpan(8, 0, 0);
TimeSpan startMeal2 = new TimeSpan(12, 0, 0);
TimeSpan endMeal2 = new TimeSpan(14, 0, 0);
TimeSpan startMeal3 = new TimeSpan(17, 0, 0);
TimeSpan endMeal3 = new TimeSpan(19, 0, 0);
int daysDiff = (timeExit - timeArrived).Days;
int meals1Count = daysDiff;
int meals2Count = daysDiff;
int meals3Count = daysDiff;
TimeSpan timeDiff = timeExit - timeArrived - TimeSpan.FromDays(daysDiff);
if (timeArrived.TimeOfDay <= endMeal1 && (timeArrived.TimeOfDay + timeDiff) >= startMeal1) meals1Count++;
if (timeArrived.TimeOfDay <= endMeal2 && (timeArrived.TimeOfDay + timeDiff) >= startMeal2) meals2Count++;
if (timeArrived.TimeOfDay <= endMeal3 && (timeArrived.TimeOfDay + timeDiff) >= startMeal3) meals3Count++;
}
I havent checked this in VS but something like this should work. I copied your same day code, assuming its correct also:
public class MealCalculation
{
int countA, countB, countC = 0;
int total = 0;
public void Calculate()
{
var start = DateTime.Now;
var finish = DateTime.Now;
// Same Day
if (start.Date == finish.Date)
{
MealsCalculate(start.Hour, start.Hour);
}
// Next Day
else if (start.AddDays(1).Date == finish.Date)
{
MealsCalculate(start.Hour, 24);
MealsCalculate(0, finish.Hour);
}
// Great Than 1 Day
else
{
// First Day
MealsCalculate(start.Hour, 24);
// Middle Full Days
var days = NumberOfDays(start.Date, finish.Date);
countA += days;
countB += days;
countC += days;
// Finish Day
MealsCalculate(0, finish.Hour);
}
// Total
total = countA + countB + countC;
}
public int NumberOfDays(DateTime start, DateTime finish)
{
var days = 0;
while (start < finish)
{
start.AddDays(1);
days++;
}
return days - 1;
}
public void MealsCalculate(int totalHoursArrived, int totalHoursExit)
{
if (totalHoursArrived <= 8 && totalHoursExit >= 17) //if date is before or on 8AM and leaves on or after 5PM.
{
countA += 1;
countB += 1;
countC += 1;
}
else if (totalHoursArrived <= 8 && (totalHoursExit >= 12 && totalHoursExit < 17)) //if date is before or on 8AM and leaves before 5PM
{
countA += 1;
countB += 1;
}
else if (totalHoursArrived <= 8 && totalHoursExit < 12) //if date is before or on 8AM and leaves before 12PM
{
countA += 1;
}
else if ((totalHoursArrived <= 12 && totalHoursArrived > 8) && totalHoursExit >= 17) //if date is before or on 12PM and leaves on or after 5PM
{
countB += 1;
countC += 1;
}
else if ((totalHoursArrived <= 12 && totalHoursArrived > 8) && totalHoursExit < 17) //if date is before or on 12PM and leaves before 5PM
{
countB += 1;
}
else if (totalHoursArrived >= 17) //if date is after or on 5PM
{
countC += 1;
}
}
}
I have a start date and an end date as well as a list of specific dates (holidays).
From the start date to the end date, first I determine if there are any weekends, if there are, I need to determine how many days are weekends and add the number of days to the end date, then determine if any of those dates fall in the list of dates in the holiday list. Again, if a date is in the holiday list, I have to take that number of days, add it to the end and run through the loop again.
For Example. Initial Date Range is 11/20/2017 to 11/24/2017. The first check for weekends would return 0, then I check for holidays, that would return 2 (the 23rd and 24th), I would then need to adjust the dates so my new range is from 11/20 to 11/26, to which I have to loop through weekends again and will find 2 days are in the weekend. Etc. The below code I cheated because I know that there are never more than 2 holidays back to back, but its very cumbersome code.
Also, please consider if the weekends come first, followed by holidays once you adjust for the initial weekend.
I never have an issue with just weekends, it is only for holidays that I am given the wrong value. The code below works fine, I am just looking to see suggestions as to how it can be improved. I have tried for loops, while loops, and this is the only scenario that seems to work. Thanks for your help.
public static int weekends(DateTime start, DateTime end, List<DateTime> holidays, Boolean leftSpace)
{
int days = 0;
DateTime tempstart = start;
TimeSpan startEnd = end - start;
int currentDays = startEnd.Days;
for (DateTime date = start; date <= end; date = date.AddDays(1))
{
int dw = (int)date.DayOfWeek;
if (dw == 0 || dw == 6)
{
days++;
}
}
if (!leftSpace)
{
end = end.AddDays(days);
if (end.DayOfWeek == 0)
{
end = end.AddDays(1);
days++;
}
else if ((int)end.DayOfWeek == 6)
{
end = end.AddDays(2);
days = days + 2;
}
Boolean holidayExist = false;
int holidayCount = 0;
foreach (DateTime holiday in holidays.Where(r => r.Date >= start.Date && r.Date <= end.Date))
{
days++;
holidayCount++;
holidayExist = true;
}
if (holidayExist)
{
DateTime tempend = end;
end = end.AddDays(holidayCount);
int newWeekend = 0;
for (DateTime date = tempend; date <= end; date = date.AddDays(1))
{
int dw = (int)date.DayOfWeek;
if (dw == 0 || dw == 6)
{
days++;
newWeekend++;
}
}
holidayExist = false;
holidayCount = 0;
DateTime holidayEnd = end.AddDays(newWeekend);
foreach (DateTime holiday in holidays.Where(r => r.Date >= end.Date && r.Date <= holidayEnd.Date))
{
days++;
holidayCount++;
holidayExist = true;
}
if (holidayExist)
{
tempend = end;
end = end.AddDays(holidayCount);
for (DateTime date = tempend; date <= end; date = date.AddDays(1))
{
int dw = (int)date.DayOfWeek;
if (dw == 0 || dw == 6)
{
days++;
}
}
}
}
}
return days;
}
I have not tested this for performance but it works. List holidays is a list of dates that are being skipped as they are holidays.
public static int weekends(DateTime start, DateTime end, List<DateTime> holidays)
{
int days = 0;
DateTime tempEnd = end;
TimeSpan startEnd = end - start;
int currentDays = startEnd.Days;
int exclusionDays = 0;
int exclWEOld = 0;
int exclHDOld = 0;
while (currentDays >= 0)
{
int excHolidayTest = exclHolidays(start, tempEnd, holidays);
int excDayTest = exclDays(start, tempEnd);
if (exclusionDays == (excDayTest+ excHolidayTest))
{
break;
}
else
{
exclusionDays = excDayTest + excHolidayTest;
if (exclWEOld == 0 && exclHDOld == 0)
{
tempEnd = tempEnd.AddDays((excDayTest + excHolidayTest));
}
else
{
tempEnd = tempEnd.AddDays((excDayTest - exclWEOld));
tempEnd = tempEnd.AddDays((excHolidayTest - exclHDOld));
}
exclWEOld = excDayTest;
exclHDOld = excHolidayTest;
}
days = excDayTest + excHolidayTest;
currentDays--;
}
return days;
}
public static int exclDays(DateTime start, DateTime end)
{
int exclusionDays = 0;
for (DateTime date = start; date <= end; date = date.AddDays(1))
{
int dw = (int)date.DayOfWeek;
if (dw == 0 || dw == 6)
{
exclusionDays++;
}
}
return exclusionDays;
}
public static int exclHolidays(DateTime start, DateTime end, List<DateTime> holidays)
{
int exclusionHolidays = 0;
foreach (DateTime holiday in holidays.Where(r => r.Date >= start.Date && r.Date <= end.Date))
{
exclusionHolidays++;
}
return exclusionHolidays;
}
I want to create a C# program that displays a calendar and uses a string that contains the names of the days:
string Names = "Sun,Mon,Tues,Wed,Thurs,Fri,Sat,";
So far I have....
int weeks = 1;
int days = 1;
string Names = "Sun,Mon,Tues,Wed,Thurs,Fri,Sat,";
string dayName;
int commaIndex = 0;
int date = 1;
while (weeks < 5)
{
while (days < 8)
{
commaIndex = Names.IndexOf(","); // find the period
dayName = Names.Remove(commaIndex);
lblCalendar.Text += dayName + "." + " " + date + " ";
Names = Names.Remove(0, commaIndex + 1);
days++;
date++;
}
weeks++;
}
But that only writes the first week.. can anybody help me figure out where there is an error?
First, you forgot to restart days variable after every iteration.
Also, after first iteration your Names string is empty.
I suggest to create array of day names and use it instead of one string.
int weeks = 1;
int days = 1;
var Names = new[] {"Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat" };
string dayName;
int commaIndex = 0;
int date = 1;
while (weeks < 5)
{
while (days < 8)
{
dayName = Names[days-1];
lblCalendar.Text += dayName + "." + " " + date + " ";
days++;
date++;
}
days = 1;
weeks++;
}
You forgot to reset days.
while (weeks < 5)
{
days = 1
string Names = "Sun,Mon,Tues,Wed,Thurs,Fri,Sat,";
while (days < 8)
{
here is a part of HTML where contains JS
<script type="text/javascript" src="/scripts/left-menu.js"></script>
This is part of code that displays at some time:
<div id="bx"></div>
And this is left-menu.js
var v = new Date();
var sec_raznica = c * 1000 - v.getTime();
var greenwich = ((v.getTimezoneOffset() * 60 * -1) - (120 * 60)) * -1000;
function t() {
var time = new Date();
time = Math.floor(time.getTime());
var t = new Date(time + sec_raznica + greenwich);
var date1 = t.getDate();
var date2 = t.getMonth() + 1;
var time1 = t.getHours();
var time2 = t.getMinutes();
var time3 = t.getSeconds();
if (date1 < 10) date1 = "0" + date1;
if (date2 < 10) date2 = "0" + date2;
if (time1 < 10) time1 = "0" + time1;
if (time2 < 10) time2 = "0" + time2;
if (time3 < 10) time3 = "0" + time3;
document.getElementById("bx").innerHTML = date1 + "." + date2 + " " + time1 + ":" + time2 + ":" + time3;
window.setTimeout("t();", 1000);
}
window.onload = t;
How do I get this data from div with id="bx" without using webBrowser?