Principales respuestas
Problemas al sacar dias entre rango de dos fechas

Pregunta
-
Public Class Cumplimiento Property Fecha As DateTime Property Porcentaje As Decimal End Class Private Sub btnAgregarGrid_Click(sender As Object, e As EventArgs) Handles btnAgregarGrid.Click Dim Lista As New List(Of Cumplimiento)() Dim FechaInicio As New DateTime(2015, 10, 1) Dim FechaFin As New DateTime(2015, 10, 31) Dim DiferenciaDias As Integer = DateDiff(DateInterval.Day, FechaInicio, FechaFin) + 1 For Index = 1 To DiferenciaDias Dim Elemento As New Cumplimiento Elemento.Fecha = FechaInicio Elemento.Porcentaje = (Index * 100) / DiferenciaDias FechaInicio = FechaInicio.AddDays(1) Lista.Add(Elemento) Next DataGridView1.DataSource = Lista End Sub
tengo el siguiente codigo que funciona pero me repite la fecha de inicio siempre y necesito sacar todos los dias no solo la fecha de inicio
el porcentaje lo veo perfectamente en la segunda columna del datagridview pero el la primera columna se me repiten la fechainicio siempre
Respuestas
-
Me sorprende que no funcione, a simple vista parece todo correcto y debería funcionar. Si quieres simplificarlo un poco a ver si así funciona, cambia el bucle para que la fecha se ponga así:
Elemento.Fecha = FechaInicio.AddDays(Index-1)
y quita la línea FechaInicio = FechaInicio.AddDays(1).
- Propuesto como respuesta Sergio Parra domingo, 8 de noviembre de 2015 19:53
- Marcado como respuesta Karen MalagónModerator lunes, 9 de noviembre de 2015 21:21
Todas las respuestas
-
Me sorprende que no funcione, a simple vista parece todo correcto y debería funcionar. Si quieres simplificarlo un poco a ver si así funciona, cambia el bucle para que la fecha se ponga así:
Elemento.Fecha = FechaInicio.AddDays(Index-1)
y quita la línea FechaInicio = FechaInicio.AddDays(1).
- Propuesto como respuesta Sergio Parra domingo, 8 de noviembre de 2015 19:53
- Marcado como respuesta Karen MalagónModerator lunes, 9 de noviembre de 2015 21:21
-
Hola, utiliza esta clase para obtener años, meses y días, por diferencia de fechas
Public Class Class1 Private monthDay As Integer() = New Integer(11) {31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} Private fromDate As DateTime Private toDate As DateTime Private year As Integer Private month As Integer Private day As Integer Public Sub New(d1 As DateTime, d2 As DateTime) Dim increment As Integer If d1 > d2 Then Me.fromDate = d2 Me.toDate = d1 Else Me.fromDate = d1 Me.toDate = d2 End If increment = 0 If Me.fromDate.Day > Me.toDate.Day Then increment = Me.monthDay(Me.fromDate.Month - 1) End If If increment = -1 Then If DateTime.IsLeapYear(Me.fromDate.Year) Then increment = 29 Else increment = 28 End If End If If increment <> 0 Then day = (Me.toDate.Day + increment) - Me.fromDate.Day increment = 1 Else day = Me.toDate.Day - Me.fromDate.Day End If If (Me.fromDate.Month + increment) > Me.toDate.Month Then Me.month = (Me.toDate.Month + 12) - (Me.fromDate.Month + increment) increment = 1 Else Me.month = (Me.toDate.Month) - (Me.fromDate.Month + increment) increment = 0 End If Me.year = Me.toDate.Year - (Me.fromDate.Year + increment) End Sub Public Overrides Function ToString() As String Return Me.year & " Año(s), " & Me.month & " Mes(es), " & Me.day & " Dia(s)" End Function Public Function ToStringYear() As String Return Me.year & " Año(s), " End Function Public Function ToStringMonth() As String Return Me.month & " Mes(es), " End Function Public Function ToStringDay() As String Return Me.day & " Dia(s)" End Function Public ReadOnly Property Years() As Integer Get Return Me.year End Get End Property Public ReadOnly Property Months() As Integer Get Return Me.month End Get End Property Public ReadOnly Property Days() As Integer Get Return Me.day End Get End Property End Class
La utilizas asi:
Dim FechaInicio As New DateTime(2015, 10, 1) Dim FechaFin As New DateTime(2015, 10, 31) Dim dateDifference As New Class1(FechaInicio, FechaFin) dateDifference.ToStringYear 'Años dateDifference.ToStringMonth 'Meses dateDifference.ToStringDay 'Dias
salu2 espero te sirva
- Editado _FOXLive domingo, 8 de noviembre de 2015 15:44