How to translate a date in a loop?

I have some meanings.

DateTime date=04/03/2015(date) Total Woring days=6 (total) Rotation Days=2 (rotationday) Shift Group=S1(this group contain two shift id 1 and 2) 

I want to start a shift for 6 days. but every 2 days Shift id 1 rotates to switch id 2 and again after a two-day shift id 2 rotates to shift id 1 and so on ... My output should look like

 04/03/2015=1 05/03/2015=1 06/03/2015=2 07/03/2015=2 08/03/2015=1 09/03/2015=1 

I get shift id through foreach loop. I tried as below, but did not get the proper result. Please help me solve this problem.

 SqlCommand cmd2 = new SqlCommand("select ShiftID from ShiftGroup where ShiftName='" + ide + "'", conn2); SqlDataAdapter sda2 = new SqlDataAdapter(cmd2); DataSet ds4 = new DataSet(); var rows2 = ds4.Tables[0].Rows; if (ds4.Tables[0].Rows.Count > 0) { foreach (DataRow row2 in rows2) { string shiftname = Convert.ToString(row2["ShiftID"]); DateTime date=04/03/2015(date) Total Woring days=6 (total) Rotation Days=2 (rotationday) DateTime rotation= date.AddDays(rotationday);// DateTime totaldate = date.AddDays(workingdays); for (DateTime rotation = date; rotation <= totaldate; rotation = rotation.AddDays(1))//total working days { //to check rotation date if (rotation <= totaldate ) { allocation.shiftallocation( rotation, shiftid); } } } 

Iโ€™ve been stuck with this from the last day, someone helps me. No need to consider my for loop, kindly provide a for loop that generates the above output

+6
source share
5 answers

You can try this,

  DateTime date=DateTime.ParseExact("04/03/2015","dd/MM/yyyy",CultureInfo.InvariantCulture); DateTime totaldate = date.AddDays(6); for (int i=0; date <= totaldate; date = date.AddDays(1),i++)//total working days { if((i/2)%2==0) Console.WriteLine(date+" "+1); else Console.WriteLine(date+" "+2); } 
+3
source
 var date = DateTime.Parse("04/03/2015"); var totalWorkingDays = 6; var rotationDays = 2; var rotationId = 1; var rotationCounter = 1; for (DateTime rotation = date; rotation <= date.AddDays(totalWorkingDays); rotation = rotation.AddDays(1)) { Console.WriteLine(rotation + " " + rotationId); if (rotationCounter++ == 2) { rotationCounter = 1; rotationId = 3 - rotationId; } } 

Or, with linq request

 var date = DateTime.Parse("04/03/2015"); var totalWorkingDays = 6; var rotationDays = 2; foreach (var d in Enumerable.Range(0, totalWorkingDays) .Select((i, index) => date.AddDays(i) + (((int)(index / rotationDays)) % 2 == 1 ? " 2" : " 1"))) { Console.WriteLine(d); } 
+1
source

Do not use dates in a loop, use abstract pointers. You really can abstract away from specific numbers and use only variables to control the result. Thus, you can easily change the number of rotationDays cycles and even arrays of work transition labels without changing the cycle.

 var date = DateTime.Parse("04/03/2015"); var totalWorkingDays = 15; var rotationDays = 2; var workshifts = new[] { "S1", "S2" }; var currentWorkshiftIndex = 0; for (int dayIndex = 0; dayIndex <= totalWorkingDays; dayIndex++) { if (dayIndex != 0 && dayIndex % rotationDays == 0) currentWorkshiftIndex = (currentWorkshiftIndex + 1) % workshifts.Length; Console.WriteLine(date.AddDays(dayIndex) + " " + workshifts[currentWorkshiftIndex]); } 
+1
source

The general decision, taking into account the holidays.

 int totalDays = 10; int rotationDays = 3; int[] shifts = new[] { 1, 2 }; string startDate = "04/03/2015"; var holidays = new List<DayOfWeek>(new[] { DayOfWeek.Saturday, DayOfWeek.Sunday, DayOfWeek.Tuesday }); var shiftAllocations = new Dictionary<DateTime, int>(); int currentShiftIndex = 0; DateTime current = DateTime.ParseExact(startDate, "dd/MM/yyyy", CultureInfo.InvariantCulture); for (int i = 0; i < totalDays; i += rotationDays) { var currentShiftId = shifts[currentShiftIndex]; // For each day from the current day till the number of shift rotation days, allocate the shift. for (int j = 0; j < rotationDays;) { // If the current day is a holiday, move to the next day by incrementing i. if (holidays.Contains(current.AddDays(i + j).DayOfWeek)) { i++; continue; } shiftAllocations.Add(current.AddDays(i + j), currentShiftId); j++; } // Increase the shift index if the value is within the bounds. If not reset the index to the beginning if ((currentShiftIndex + 1) >= shifts.Length) currentShiftIndex = 0; else currentShiftIndex++; } foreach (var kvp in shiftAllocations) { Console.WriteLine(kvp.Key.ToString("dd/MM/yyyy") + ":" + kvp.Value); } 
+1
source

According to your comment for 5 working days a week, I wrote the following:

  var start = new DateTime(2015, 04, 03); var working_day_count = 6; var rotation_interval = 2; var shifts = new List<int> { 1, 2 }; var rotation_count = 1; var shift_index = 0; for (var i = 0; i < working_day_count; i++) { while ((start.DayOfWeek == DayOfWeek.Saturday) || (start.DayOfWeek == DayOfWeek.Sunday)) { start = start.AddDays(1); } Console.WriteLine(start.ToString() + " = " + shifts[shift_index].ToString()); start = start.AddDays(1); rotation_count++; if (rotation_count > rotation_interval) { rotation_count = 1; shift_index++; if (shift_index >= shifts.Count) { shift_index = 0; } } } 

Just change the values โ€‹โ€‹of the first four varibales as you like. I tried to simplify understanding, instead of performing or compactly.

+1
source

Source: https://habr.com/ru/post/983297/


All Articles