locked
Automatic Compute date RRS feed

  • Question

  • Hi Everyone

    any ideas how to code the automatic compute the date. Example

    In Date Applied the Date today February 18, 2012 then will be automtic compute in 10 days in Date Realesed February 28, 2012.

    i have code for the date 

    this is for the DateApplied: Label26.Text = Format(Now, "MMMM dd, yyyy")

    Pls Help me Thanks.

    Saturday, February 18, 2012 11:42 AM

Answers

  • To calculate Date Released, you can use Date.Now.AddDays(10).
     

    --
    Mike
    • Proposed as answer by Cor Ligthert Saturday, February 18, 2012 12:21 PM
    • Marked as answer by WITWEW Saturday, February 18, 2012 12:21 PM
    Saturday, February 18, 2012 12:11 PM
  • If you want to get the date that is 10 days into the future, you could use:

    Label26.Text = Date.Now.AddDays(10).ToString("MMMM dd, yyyy")

    Did I understand you correctly?


    If a post is helpful to you or solves a problem, remember to mark it as answer, propose it as answer or vote up. Check out my development so far; http://turl.no/jeb


    • Edited by bilde2910 Saturday, February 18, 2012 12:17 PM Fixed the date format to match your needs better
    • Marked as answer by WITWEW Saturday, February 18, 2012 12:21 PM
    Saturday, February 18, 2012 12:15 PM
  • I believe you need to impliment it yourself, which is fairly easy.  Examples are included in this discussion.

    --
    Mike
    • Marked as answer by WITWEW Saturday, February 18, 2012 12:43 PM
    Saturday, February 18, 2012 12:29 PM
  • Sorry, forgot which group I was in... 
     
    Untested, but I converted the code using the site: http://www.developerfusion.com/tools/convert/csharp-to-vb/
     
     
    Public NotInheritable Class DateTimeExtensions
     Private Sub New()
     End Sub
     <System.Runtime.CompilerServices.Extension> _
     Public Shared Function AddWorkDays([date] As DateTime, workingDays As Integer) _
        As DateTime
      Dim direction As Integer = If(workingDays < 0, -1, 1)
      Dim newDate As DateTime = [date]
      While workingDays <> 0
       newDate = newDate.AddDays(direction)
       If newDate.DayOfWeek <> DayOfWeek.Saturday _
        AndAlso newDate.DayOfWeek <> DayOfWeek.Sunday AndAlso Not newDate.IsHoliday() Then
        workingDays -= direction
       End If
      End While
      Return newDate
     End Function
     
     <System.Runtime.CompilerServices.Extension> _
     Public Shared Function IsHoliday([date] As DateTime) As Boolean
      ' You'd load/cache from a DB or file somewhere rather than hardcode
      Dim holidays As DateTime() = New DateTime() _
        {New DateTime(2010, 12, 27), New DateTime(2010, 12, 28), _
        New DateTime(2011, 1, 3), New DateTime(2011, 1, 12), New DateTime(2011, 1, 13)}
     
      Return holidays.Contains([date].[Date])
     End Function
    End Class

    --
    Mike
    • Marked as answer by WITWEW Saturday, February 18, 2012 1:06 PM
    Saturday, February 18, 2012 1:00 PM
  • hi Thanks for helping me.i ask another question how about will not count the saturday and sunday?only compute the working days only.

    In case this might help also:

        Private holidayDates As New List(Of DateTime)
       
    
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
            Handles MyBase.Load
    
            ' Add the dates for the holidays to skip here
    
            Dim test As DateTime = SkipDays(-10)
            Stop
    
        End Sub
    
    
    
        Private Function SkipDays(ByVal daysToSkipTo As Integer) As DateTime
    
            Dim retVal As DateTime
            Dim skipToDate As DateTime = Today.Date
            Dim cnt As Integer = 0
            Dim addDays As Integer = 0
    
            If daysToSkipTo = 0 Then
                retVal = Today.Date
            ElseIf daysToSkipTo > 0 Then
                addDays = 1
            Else
                addDays = -1
            End If
    
            Do Until cnt = Math.Abs(daysToSkipTo)
                skipToDate = skipToDate.Date.AddDays(addDays)
    
                If skipToDate.DayOfWeek <> DayOfWeek.Saturday AndAlso _
                   skipToDate.DayOfWeek <> DayOfWeek.Sunday AndAlso Not _
                   holidayDates.Contains(skipToDate) Then
                    cnt += 1
                End If
            Loop
    
            retVal = skipToDate
    
            Return retVal
    
        End Function

    • Marked as answer by WITWEW Sunday, February 19, 2012 1:12 PM
    Saturday, February 18, 2012 2:29 PM

All replies

  • To calculate Date Released, you can use Date.Now.AddDays(10).
     

    --
    Mike
    • Proposed as answer by Cor Ligthert Saturday, February 18, 2012 12:21 PM
    • Marked as answer by WITWEW Saturday, February 18, 2012 12:21 PM
    Saturday, February 18, 2012 12:11 PM
  • If you want to get the date that is 10 days into the future, you could use:

    Label26.Text = Date.Now.AddDays(10).ToString("MMMM dd, yyyy")

    Did I understand you correctly?


    If a post is helpful to you or solves a problem, remember to mark it as answer, propose it as answer or vote up. Check out my development so far; http://turl.no/jeb


    • Edited by bilde2910 Saturday, February 18, 2012 12:17 PM Fixed the date format to match your needs better
    • Marked as answer by WITWEW Saturday, February 18, 2012 12:21 PM
    Saturday, February 18, 2012 12:15 PM
  • hi Thanks for helping me.i ask another question how about will not count the saturday and sunday?only compute the working days only.
    Saturday, February 18, 2012 12:22 PM
  • and other holiday example christmas will not compute.
    Saturday, February 18, 2012 12:27 PM
  • I believe you need to impliment it yourself, which is fairly easy.  Examples are included in this discussion.

    --
    Mike
    • Marked as answer by WITWEW Saturday, February 18, 2012 12:43 PM
    Saturday, February 18, 2012 12:29 PM
  • thanks mike. there is converter to c# to vb2010?
    Saturday, February 18, 2012 12:44 PM
  • Sorry, forgot which group I was in... 
     
    Untested, but I converted the code using the site: http://www.developerfusion.com/tools/convert/csharp-to-vb/
     
     
    Public NotInheritable Class DateTimeExtensions
     Private Sub New()
     End Sub
     <System.Runtime.CompilerServices.Extension> _
     Public Shared Function AddWorkDays([date] As DateTime, workingDays As Integer) _
        As DateTime
      Dim direction As Integer = If(workingDays < 0, -1, 1)
      Dim newDate As DateTime = [date]
      While workingDays <> 0
       newDate = newDate.AddDays(direction)
       If newDate.DayOfWeek <> DayOfWeek.Saturday _
        AndAlso newDate.DayOfWeek <> DayOfWeek.Sunday AndAlso Not newDate.IsHoliday() Then
        workingDays -= direction
       End If
      End While
      Return newDate
     End Function
     
     <System.Runtime.CompilerServices.Extension> _
     Public Shared Function IsHoliday([date] As DateTime) As Boolean
      ' You'd load/cache from a DB or file somewhere rather than hardcode
      Dim holidays As DateTime() = New DateTime() _
        {New DateTime(2010, 12, 27), New DateTime(2010, 12, 28), _
        New DateTime(2011, 1, 3), New DateTime(2011, 1, 12), New DateTime(2011, 1, 13)}
     
      Return holidays.Contains([date].[Date])
     End Function
    End Class

    --
    Mike
    • Marked as answer by WITWEW Saturday, February 18, 2012 1:06 PM
    Saturday, February 18, 2012 1:00 PM
  • thanks mike
    Saturday, February 18, 2012 1:06 PM
  • hi Thanks for helping me.i ask another question how about will not count the saturday and sunday?only compute the working days only.

    In case this might help also:

        Private holidayDates As New List(Of DateTime)
       
    
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
            Handles MyBase.Load
    
            ' Add the dates for the holidays to skip here
    
            Dim test As DateTime = SkipDays(-10)
            Stop
    
        End Sub
    
    
    
        Private Function SkipDays(ByVal daysToSkipTo As Integer) As DateTime
    
            Dim retVal As DateTime
            Dim skipToDate As DateTime = Today.Date
            Dim cnt As Integer = 0
            Dim addDays As Integer = 0
    
            If daysToSkipTo = 0 Then
                retVal = Today.Date
            ElseIf daysToSkipTo > 0 Then
                addDays = 1
            Else
                addDays = -1
            End If
    
            Do Until cnt = Math.Abs(daysToSkipTo)
                skipToDate = skipToDate.Date.AddDays(addDays)
    
                If skipToDate.DayOfWeek <> DayOfWeek.Saturday AndAlso _
                   skipToDate.DayOfWeek <> DayOfWeek.Sunday AndAlso Not _
                   holidayDates.Contains(skipToDate) Then
                    cnt += 1
                End If
            Loop
    
            retVal = skipToDate
    
            Return retVal
    
        End Function

    • Marked as answer by WITWEW Sunday, February 19, 2012 1:12 PM
    Saturday, February 18, 2012 2:29 PM
  • I meant to also say that the example in the form's load event was a test for a negative number of days to skip. In your instance, you'd want to use 10, not -10. Hopefully that makes sense. :-0
    Saturday, February 18, 2012 2:31 PM