Resultant Date will not cross the Current Month and will not be Sunday in VB.NET ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SANDEEP PREMCHANDANI
    New Member
    • Aug 2010
    • 3

    Resultant Date will not cross the Current Month and will not be Sunday in VB.NET ?

    How do we add 15 days to the current Date which will not cross the month
    and resultant Date will never be Sunday? If it is Sunday make it Saturday?

    Example > If Current Date is 17/08/2010 (DD/MM/YYYY)
    Then Resultant Date is 31/08/2010 (DD/MM/YYYY) [Donot Cross the Current Month.]
    Also 31/08/2010 would never be Sunday.
    If it is Sunday make it 30/08/2010 (Saturday). [Date is not Sunday.]
  • Joseph Martell
    Recognized Expert New Member
    • Jan 2010
    • 198

    #2
    Your logic is pretty straight-forward. The DateTime class of the .Net framework would provide everything you need (including a DayOfWeek property).

    There is no built-in function that provides this behaviour, however, so you will have to code the logic yourself.

    If you have specific questions about coding this then we can help more, but give us an example of what you have tried first. You have an algorithm already stated to work off of though.

    Comment

    • richkid
      New Member
      • Apr 2007
      • 28

      #3
      Code:
              Dim currDate, advDate As Date
              Dim currDay, lastDay As Integer
      
              currDate = Me.DateTimePicker1.Value.Date
              advDate = currDate.AddDays(15)
              If Not advDate.Month = currDate.Month Then ' Goes over to other month
                  'message that date crosses the current month
                  'or set to last date of mth
                  currDay = currDate.Day
                  lastDay = Date.DaysInMonth(currDate.Year, currDate.Month)
                  advDate = currDate.AddDays(lastDay - currDay)
              End If
      
              If advDate.DayOfWeek = 0 Then ' If Sunday
                  advDate = advDate.AddDays(-1) ' set to Saturday
              End If
              TextBox1.Text = advDate.ToLongDateString
          End Sub
      Last edited by Frinavale; Aug 4 '10, 05:21 PM. Reason: Fixed code tags.

      Comment

      Working...