Results 1 to 8 of 8

Thread: Help on Date Validation

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2008
    Location
    Philippines
    Posts
    43

    Help on Date Validation

    Date1 = 4-10-2008
    Date2 = 4-14-2008

    I want to count the number of days from date1 to date2... My problem is i want to count only working days not to include Sunday and Saturday... How do i know if the date falls on Saturday and Sunday? Thnks

  2. #2

  3. #3

    Thread Starter
    Member
    Join Date
    Apr 2008
    Location
    Philippines
    Posts
    43

    Smile Re: Help on Date Validation

    here is my code:

    Dim nCount As Integer, i As Integer
    Dim dFrom As Date, dTo As Date
    Dim temp As Date
    Dim nDay As Integer

    If IsDate(txtdFrom.Text) Then
    txtdFrom.Text = Format(txtdFrom.Text, "DD-MMM-YY")
    If IsDate(txtdTo.Text) Then
    txtdTo.Text = Format(txtdTo.Text, "DD-MMM-YY")
    dFrom = txtdFrom.Text
    dTo = txtdTo.Text
    Do Until dFrom = dTo
    dFrom = dFrom + 1
    temp = Format(dFrom, "DDD")
    If temp = "Sat" Then
    nCount = nCount
    Else
    nCount = nCount + 1
    End If
    Loop

    nDay = (dTo - dFrom) + 1

    txtLeaveDays.Text = nDay - nCount
    End If
    End If

    i got an error data type mismatch [If temp = "Sat" then]

    any help pls?

  4. #4
    Frenzied Member Phill64's Avatar
    Join Date
    Jul 2005
    Location
    Queensland, Australia
    Posts
    1,201

    Re: Help on Date Validation

    because your temp variable is declared as a Date, yo ucan't shove a string into it.

    Look at the msdn sample given to you, there is a property DayOfWeek which tells you in enumerator form, which day it is, therefore you could just do a loop, keep adding a day to the date, and checking if it is a weekend or not.

    I was bored so here you go, i wrote a function, according to it there are 2 business days between the dates you mentioned, is that right?
    Code:
        Private Function GetBusinessDays(ByVal date1 As Date, ByVal date2 As Date) As Integer
            If date1 > date2 Then 'shuffle dates to make sure date2 is the biggest
                Dim date3 As Date = date1
                date1 = date2
                date2 = date3
            End If
    
            'loop, adding days to the date until it matches date2
            Dim bDays As Integer = 0
            Do While date1 < date2
                date1 = date1.AddDays(1)
                If Not (date1.DayOfWeek = DayOfWeek.Saturday Or date1.DayOfWeek = DayOfWeek.Sunday) Then bDays += 1
            Loop
    
            Return bDays
        End Function

  5. #5

    Thread Starter
    Member
    Join Date
    Apr 2008
    Location
    Philippines
    Posts
    43

    Re: Help on Date Validation

    how do i code this in visual basic 6.0... i think this code is for .net... can u help pls in vb6.. Thnks

  6. #6

    Thread Starter
    Member
    Join Date
    Apr 2008
    Location
    Philippines
    Posts
    43

    Re: Help on Date Validation

    im doing an online leave application... Now if u file a leave between two dates it should validate if it falls on sat or sun... If it falls on weekend the counting of days should not be included.
    Urgent help ...

  7. #7
    Frenzied Member Phill64's Avatar
    Join Date
    Jul 2005
    Location
    Queensland, Australia
    Posts
    1,201

    Re: Help on Date Validation

    There is a VB6 forum, this is the VB.NET forum

    http://www.vbforums.com/forumdisplay.php?f=1

  8. #8

    Thread Starter
    Member
    Join Date
    Apr 2008
    Location
    Philippines
    Posts
    43

    Re: Help on Date Validation

    thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width