Click here to Skip to main content
14,239,876 members
Rate this:
Please Sign up or sign in to vote.
See more:
Good Morning,
I'm working on a project where the user adds a date in a textbox and the program creates a list of activities with initial and final dates, also creates activities depending on the number of reels Ci, what I'm looking for is that the program creates the list of activities with dates from Monday to Friday or Monday to Saturday or Monday to Sunday, because if you do not do this the program calculates the days without conditions.

How can i solve this?


 Dim Table As New DataTable
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim date1 As Date
        Try
            date1 = CType(TextBox1.Text, Date)
        Catch
            Exit Sub
        End Try
                Dim numRows As Integer

                If Integer.TryParse(TextBox4.Text, numRows) Then
                    If (numRows >= 1) Then
                For i As Integer = 1 To numRows
                    Table.Rows.Add("Manufacturing", "Channel", "Cut plate C" & i, DateAdd(DateInterval.Day, 1, date1), DateAdd(DateInterval.Day, 2, date1))
                    Table.Rows.Add("Manufacturing", "Channel", "Rolled plate C" & i, DateAdd(DateInterval.Day, 3, date1), DateAdd(DateInterval.Day, 4, date1))
                    Table.Rows.Add("Manufacturing", "Channel", "Longituid welding C" & i, DateAdd(DateInterval.Day, 5, date1), DateAdd(DateInterval.Day, 6, date1))
                    Table.Rows.Add("Manufacturing", "Channel", "Re rolled C" & i, DateAdd(DateInterval.Day, 7, date1), DateAdd(DateInterval.Day, 8, date1))
                    date1 = DateAdd(DateInterval.Day, 8, date1)
                Next
                    Else
                        MessageBox.Show("Value: " & TextBox4.Text & " El numero de filas debe ser mayor o igual a 1", "Error")
                    End If
                Else
                    MessageBox.Show("Value: " & TextBox4.Text & " Número invalido", "Error")
                End If

                For C As Integer = 0 To DataGridView1.Columns.Count - 1
                    DataGridView1.Columns(C).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
                Next C

    End Sub


What I have tried:

Dim Eleccion As Integer = 0
  Private Sub RBEleccionXXX_CheckedChanged(sender As Object, e As EventArgs) Handles RBEleccion1.CheckedChanged, RBEleccion2.CheckedChanged, RBEleccion3.CheckedChanged
      Select Case sender.name
          Case "RBEleccion1"
              Eleccion = 1
          Case "RBEleccion2"
              Eleccion = 2
          Case "RBEleccion3"
              Eleccion = 3
      End Select


Select Case Eleccion                ' Los días son Domigo = 0, Lunes = 1, …, Sabado = 6.
    Case 1
        If date1.DayOfWeek = 6 Then
            date1 = date1.AddDays(2)
        ElseIf date1.DayOfWeek = 0 Then
            date1 = date1.AddDays(1)
        End If
    Case 2
        If date1.DayOfWeek = 0 Then
            date1 = date1.AddDays(1)
        End If
    Case 3
        ' Nada hacer
End Select
Posted
Updated 10-Jul-19 0:01am
Comments
ZurdoDev 8-Jul-19 16:59pm
   
Easy. Check the day of the week and then either add it or don't. Where are you stuck?
Maciej Los 9-Jul-19 16:37pm
   
Your question is not clear. Please, be more specific and provide more details about your issue. What kind of condition you need to define to calculate dates.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Assuming that all activities start from the next of today and every...
Assuming that every single date of activity depends on previous date...

You can create simple class (called "ManufacturingActivities") which gets initial date and returns a set of date of activities. For example:

Public Class ManufacturingActivities
	
	Public Property CutPlateC As Date 
	Public Property RolledPlateC As Date 
	Public Property LongituidWeldingC As Date 
	Public Property ReRolledC As Date
	
	Public Sub New(initialDate As Date)
		
		CutPlateC = GetNextWorkingDay(initialDate.AddDays(1))
		RolledPlateC = GetNextWorkingDay(CutPlateC.AddDays(1))
		LongituidWeldingC = GetNextWorkingDay(RolledPlateC.AddDays(1))
		ReRolledC = GetNextWorkingDay(LongituidWeldingC.AddDays(1))
		
	End Sub

	Private Function GetNextWorkingDay(dateToCheck As Date) As Date
		
		Dim retVal As Date = dateToCheck
		
		Do While retVal.DayOfWeek = DayOfWeek.Saturday Or retVal.DayOfWeek = DayOfWeek.Sunday
			retVal = retVal.AddDays(1)
		Loop
		
		Return retVal
		
	End Function

End Class


Usage:
Dim initialdate As Date = DateTime.Today

Dim ma As ManufacturingActivities = New ManufacturingActivities(initialDate)

Console.WriteLine($"CutPlateC = {ma.CutPlateC.ToString("yyyy-MM-dd")}")
Console.WriteLine($"RolledPlateC = {ma.RolledPlateC.ToString("yyyy-MM-dd")}")
Console.WriteLine($"LongituidWeldingC = {ma.LongituidWeldingC.ToString("yyyy-MM-dd")}")
Console.WriteLine($"ReRolledC = {ma.ReRolledC.ToString("yyyy-MM-dd")}")


Result:
CutPlateC = 2019-07-11
RolledPlateC = 2019-07-12
LongituidWeldingC = 2019-07-15
ReRolledC = 2019-07-16


As you can see, saturday (13.) and sunday (14.) of July have been skipped.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100