Comparing AM and PM in C# - c#

Im comparing time. See I have this code
DateTime t1 = DateTime.Now;
DateTime StartMorningCleaning = Convert.ToDateTime("6:00:00 AM");
DateTime EndMorningCleaning = Convert.ToDateTime("6:59:59 AM");
DateTime StartMorning = Convert.ToDateTime("7:00:00 AM");
DateTime EndMorning = Convert.ToDateTime("6:59:59 PM");
DateTime StartNightCleaning = Convert.ToDateTime("7:00:00 PM");
DateTime EndNightCleaning = Convert.ToDateTime("7:59:59 PM");
DateTime StartNight = Convert.ToDateTime("8:00:00 PM");
DateTime BeforeMidnight = Convert.ToDateTime("11:59:59 PM");
DateTime Midnight = Convert.ToDateTime("12:00:00 AM");
DateTime EndNight = Convert.ToDateTime("5:59:59 AM");
Im trying to compare the current time if its in between Start and end times. The problem is when i go the the PM part it doesnt work and i have no idea why. Starting from StarNightCleaning up to EndNight. Any ideas why? here is my code in the comparing section
if (t1 >= StartMorningCleaning && t1 <= EndMorningCleaning)
{
MessageBox.Show("Morning Cleaning Time");
CleaningTime = false;
}
else if (t1 >= StartMorning && t1 <= EndMorning)
{
MessageBox.Show("MorningTour");
CleaningTime = true;
}
else if (t1 >= StartNightCleaning && t1 <= EndNightCleaning)
{
MessageBox.Show("Night Cleaning Time");
CleaningTime = false;
}
else if (t1 >= StartNight && t1 <= BeforeMidnight)
{
MessageBox.Show("NightTour");
CleaningTime = true;
}
else if (t1 >= Midnight && t1 <= EndNight)
{
MessageBox.Show("NightTour");
CleaningTime = true;
}

Use Hour property and try reduce logic to numbers.
int t1 = DateTime.Now.Hour;
if (t1 >= 6 && t1 < 7) {
MessageBox.Show("Morning Cleaning Time");
CleaningTime = false;
} else if (t1 >= 7 t1 < 19) {
MessageBox.Show("MorningTour");
CleaningTime = true;
} else if (t1 >= 19 && t1 < 20) { //PM
MessageBox.Show("Night Cleaning Time");
CleaningTime = false;
} else if (t1 >= 20 && t1 <= 23) { //PM
MessageBox.Show("NightTour");
CleaningTime = true;
} else if (t1 >= 0 && t1 <= 5) { //AM - this if can be just else (remove if part)
MessageBox.Show("NightTour");
CleaningTime = true;
}

Related

Add empty index when number of index is less then 7

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.

How to keep count between specific times between two dates

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;
}
}
}

Comparing Range of DateTimes in a Loop

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;
}

How to cycle with dates?

I would need to color the days of a months using a datepicker with the following logic: starting from a date in database, i would like to color in orange next 8 days, then next 2 days in green and so on for every following month.
I have the following code but i need a loop:
private void calFecha_DayRender(object source, DayRenderEventArgs e)
{
if (ddloperadores.SelectedValue != "Todos")
{
DataTable asistencia = OperadoresForaneosAsistencia((int)WAPS.Globals.ConvertTo(txtNumOperador.Text, 0)).Tables[0];
if (asistencia.Rows.Count > 0)
{
DataRow iRow = asistencia.Rows[0];
string Tipo = iRow["TipoJornada"].ToString();
Tipo = asistencia.Rows[0]["TipoJornada"].ToString();
if (Tipo == "1") //Tipos de Jornadas
{
DateTime FechaJornada = Convert.ToDateTime(iRow["Jornada_Ini"]);
DateTime fechatemp = DateTime.Today;
DateTime primero = new DateTime(fechatemp.Year, fechatemp.Month, 1);
DateTime ultimo = new DateTime(fechatemp.Year, fechatemp.Month + 1, 1).AddDays(-1);
int counter = 1;
for (DateTime n = FechaJornada; n <= ultimo; n=n.AddDays(1))
{
int rem = counter % 10;
if (rem >= 1 && rem <= 8)
{
e.Cell.BackColor = System.Drawing.Color.DarkGreen;
}
else
{
e.Cell.BackColor = System.Drawing.Color.DarkOrange;
}
counter++;
}
}
}
}
}
calendar image
In reference of your comment do the following in the loop:
int counter = 1;
for(n = FechaJornada; n <= ultimo; n.AddDay(1))
{
int rem = counter % 10;
if (rem >= 1 && rem <= 8)
e.Cell.BackColor = System.Drawing.Color.DarkGreen;
else
e.Cell.BackColor = System.Drawing.Color.DarkOrange;
counter++;
}
EDIT: after moving in dayrender
// compute FechaJornada then...
if(FechaJornada < e.Day.Date)
{
int rem = (e.Day.Date - FechaJornada).Days % 10;
if (rem >= 1 && rem <= 8)
e.Cell.BackColor = System.Drawing.Color.DarkGreen;
else
e.Cell.BackColor = System.Drawing.Color.DarkOrange;
}
else
e.Cell.BackColor = System.Drawing.Color.White;

C# Comparing dates with converted DatePicker

I have an issue with this piece of code it doesn't seem to work what I am doing wrong?
DepartureDate = DatePicked
DateTime today = DateTime.Today;
DateTime seven = DateTime.Today.AddDays(+7);
DateTime dt = Convert.ToDateTime(DepartureDate);
if (columnName == "DepartureDate")
{
//range between today and seven days ahead +7
if (dt >= today || dt <= seven)
{
result = valid;
return result;
}
//can't be in the past
if (dt < today)
{
result = "Can't be in the past";
return result;
}
}
Your check for being within the range is using OR instead of AND.
Try changing:
if (dt >= today || dt <= seven)
to:
if (dt >= today && dt <= seven)
Change
if (dt >= today || dt <= seven)
to
if (dt >= today && dt <= seven)

Categories

Resources