Day Finder Program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anatomycut
    New Member
    • Jul 2007
    • 5

    Day Finder Program

    Hi,

    I'm very new to VB and need help please. I want to build a 'Day Finder' program that tells you the day for a given date between 1600 & 2100 and whether it is past, present or future. I have coded the variables and algorithm for a form with 3 text box entries and a command button . How do I get VB to recognise whether the date is past, present or future i.e. by looking to today's date? Is there any existing VB code out there? (i've had a look and could only find a c++ program which doesn't help), thanks
  • ilearneditonline
    Recognized Expert New Member
    • Jul 2007
    • 130

    #2
    can you provide a code snippet of what you have done thus far?

    Comment

    • Mague
      New Member
      • May 2007
      • 137

      #3
      Originally posted by ilearneditonlin e
      can you provide a code snippet of what you have done thus far?

      Im assuming you are using vb.net cause you said you were new

      I dont know excally what you want it to do but hope this helps.

      Dim ts As TimeSpan
      ts = (CType(DateTime Picker1.Value.S ubtract(Today), TimeSpan))
      If ts.TotalDays < 1 Then
      Label1.Text = "Timespan: Past Date or Today's Date"
      Else : Label1.Text = "Timespan: Future Date"
      End If

      I found this on google. I dont understand it but you might want to

      The website is
      http://blogs.vbcity.co m/xtab/archive/2005/12/26/5755.aspx

      Hope this helps
      Mague

      Comment

      • pureenhanoi
        New Member
        • Mar 2007
        • 175

        #4
        Originally posted by anatomycut
        Hi,

        I'm very new to VB and need help please. I want to build a 'Day Finder' program that tells you the day for a given date between 1600 & 2100 and whether it is past, present or future. I have coded the variables and algorithm for a form with 3 text box entries and a command button . How do I get VB to recognise whether the date is past, present or future i.e. by looking to today's date? Is there any existing VB code out there? (i've had a look and could only find a c++ program which doesn't help), thanks
        Subtraction two date directly. If the result < 0 then the date is in the past. If the result >0 then the date is in future.
        If ur data was not already the DateTime Type, so, convert it into DateTime first
        Example
        Date - (Date+1) will return -1
        Date - (Date-1) will return +1
        (urDate - Date ) < 0 then urDate is in the past
        Last edited by pureenhanoi; Jul 14 '07, 09:28 AM. Reason: adds info

        Comment

        • anatomycut
          New Member
          • Jul 2007
          • 5

          #5
          great, thanks all. this is a DateDiff function which i want to use e.g:

          Private Function IsPPOrF(ByVal strDate As String) As String
          If Not IsDate(strDate) Then
          IsPPOrF = "Bad date value"

          Else
          Select Case DateDiff("d", Now, strDate)
          Case 0
          IsPPOrF = "Present"
          Case Is < 0
          IsPPOrF = "Past"
          Case Is > 0
          IsPPOrF = "Future"
          End Select
          End If
          End Function

          how would i add that to my code (which is awful but just about works) so my msgbox says "the date on the 14 jun 1977 is a friday past/present/future"? (it is down at the bottom)

          Public Class Gregorian_Calen dar_2

          'Declare variables

          Dim Day As Integer
          Dim dayNumber As Integer
          Dim Month As String
          Dim MonthCase As Integer
          Dim Year As Integer


          Private Sub HScrollBar1_Scr oll(ByVal sender As System.Object, ByVal e As System.Windows. Forms.ScrollEve ntArgs) Handles HScrollBar1.Scr oll

          'Horizontal Scroll Bar gives the day of the month'

          Dim HScrollBar As Integer
          HScrollBar = HScrollBar1.Val ue
          txtDay.Text = HScrollBar

          End Sub

          Private Sub txtYear_TextCha nged(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles txtYear.TextCha nged

          'Only 4 digit years between 1900 and 2100 are allowed

          Year = txtYear.Text
          If Year < 1900 Or Year > 2100 Then MsgBox(" Please enter a year between 1900 and 2100 ")

          End Sub


          Private Sub btnFindDay_Clic k(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles btnFindDay.Clic k

          'Match variables to user input

          Month = cbMonth.Text
          Day = txtDay.Text

          'Assign numeric values to combo box items'

          Select Case Month
          Case "Jan"
          MonthCase = 1
          Case "Feb"
          MonthCase = 2
          Case "Mar"
          MonthCase = 3
          Case "Apr"
          MonthCase = 4
          Case "May"
          MonthCase = 5
          Case "Jun"
          MonthCase = 6
          Case "Jul"
          MonthCase = 7
          Case "Aug"
          MonthCase = 8
          Case "Sep"
          MonthCase = 9
          Case "Oct"
          MonthCase = 10
          Case "Nov"
          MonthCase = 11
          Case "Dec"
          MonthCase = 12

          End Select

          'Algorithm to use for months January or February: calculates a value 0 to 6 for Saturday to Friday

          If Month = "Jan" Or Month = "Feb" Then

          dayNumber = ((Day + (2 * MonthCase) + (0.6 * (MonthCase + 1)) _
          + Year + (Year / 4) - (Year / 100) + ((Year / 400) + 2)) Mod 7) - 1

          Else

          'Algorithm to use for months March to December: calculates a value 0 to 6 for Saturday to Friday

          dayNumber = (Day + (2 * (MonthCase + 12)) + (0.6 * (MonthCase + 13)) _
          + (Year - 1) + ((Year - 1) / 4) - ((Year - 1) / 100 + (((Year - 1) / 400) + 2))) Mod 7

          End If

          'Tell the user what day it is from the date entered

          If dayNumber = 0 Then MsgBox(" The day on the " & Day & " " & Month & " " & Year & " is a Saturday ")
          If dayNumber = 1 Then MsgBox(" The day on the " & Day & " " & Month & " " & Year & " is a Sunday ")
          If dayNumber = 2 Then MsgBox(" The day on the " & Day & " " & Month & " " & Year & " is a Monday ")
          If dayNumber = 3 Then MsgBox(" The day on the " & Day & " " & Month & " " & Year & " is a Tuesday ")
          If dayNumber = 4 Then MsgBox(" The day on the " & Day & " " & Month & " " & Year & " is a Wednesday ")
          If dayNumber = 5 Then MsgBox(" The day on the " & Day & " " & Month & " " & Year & " is a Thursday ")
          If dayNumber = 6 Then MsgBox(" The day on the " & Day & " " & Month & " " & Year & " is a Friday ")

          End Sub

          'Exit Program

          Private Sub btnExit_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles btnExit.Click

          End

          End Sub
          End Class

          Comment

          • pureenhanoi
            New Member
            • Mar 2007
            • 175

            #6
            Originally posted by anatomycut
            great, thanks all. this is a DateDiff function which i want to use e.g:

            Private Function IsPPOrF(ByVal strDate As String) As String
            If Not IsDate(strDate) Then
            IsPPOrF = "Bad date value"

            Else
            Select Case DateDiff("d", Now, strDate)
            Case 0
            IsPPOrF = "Present"
            Case Is < 0
            IsPPOrF = "Past"
            Case Is > 0
            IsPPOrF = "Future"
            End Select
            End If
            End Function

            how would i add that to my code (which is awful but just about works) so my msgbox says "the date on the 14 jun 1977 is a friday past/present/future"? (it is down at the bottom)

            Public Class Gregorian_Calen dar_2

            'Declare variables

            Dim Day As Integer
            Dim dayNumber As Integer
            Dim Month As String
            Dim MonthCase As Integer
            Dim Year As Integer


            Private Sub HScrollBar1_Scr oll(ByVal sender As System.Object, ByVal e As System.Windows. Forms.ScrollEve ntArgs) Handles HScrollBar1.Scr oll

            'Horizontal Scroll Bar gives the day of the month'

            Dim HScrollBar As Integer
            HScrollBar = HScrollBar1.Val ue
            txtDay.Text = HScrollBar

            End Sub

            Private Sub txtYear_TextCha nged(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles txtYear.TextCha nged

            'Only 4 digit years between 1900 and 2100 are allowed

            Year = txtYear.Text
            If Year < 1900 Or Year > 2100 Then MsgBox(" Please enter a year between 1900 and 2100 ")

            End Sub


            Private Sub btnFindDay_Clic k(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles btnFindDay.Clic k

            'Match variables to user input

            Month = cbMonth.Text
            Day = txtDay.Text

            'Assign numeric values to combo box items'

            Select Case Month
            Case "Jan"
            MonthCase = 1
            Case "Feb"
            MonthCase = 2
            Case "Mar"
            MonthCase = 3
            Case "Apr"
            MonthCase = 4
            Case "May"
            MonthCase = 5
            Case "Jun"
            MonthCase = 6
            Case "Jul"
            MonthCase = 7
            Case "Aug"
            MonthCase = 8
            Case "Sep"
            MonthCase = 9
            Case "Oct"
            MonthCase = 10
            Case "Nov"
            MonthCase = 11
            Case "Dec"
            MonthCase = 12

            End Select

            'Algorithm to use for months January or February: calculates a value 0 to 6 for Saturday to Friday

            If Month = "Jan" Or Month = "Feb" Then

            dayNumber = ((Day + (2 * MonthCase) + (0.6 * (MonthCase + 1)) _
            + Year + (Year / 4) - (Year / 100) + ((Year / 400) + 2)) Mod 7) - 1

            Else

            'Algorithm to use for months March to December: calculates a value 0 to 6 for Saturday to Friday

            dayNumber = (Day + (2 * (MonthCase + 12)) + (0.6 * (MonthCase + 13)) _
            + (Year - 1) + ((Year - 1) / 4) - ((Year - 1) / 100 + (((Year - 1) / 400) + 2))) Mod 7

            End If

            'Tell the user what day it is from the date entered

            If dayNumber = 0 Then MsgBox(" The day on the " & Day & " " & Month & " " & Year & " is a Saturday ")
            If dayNumber = 1 Then MsgBox(" The day on the " & Day & " " & Month & " " & Year & " is a Sunday ")
            If dayNumber = 2 Then MsgBox(" The day on the " & Day & " " & Month & " " & Year & " is a Monday ")
            If dayNumber = 3 Then MsgBox(" The day on the " & Day & " " & Month & " " & Year & " is a Tuesday ")
            If dayNumber = 4 Then MsgBox(" The day on the " & Day & " " & Month & " " & Year & " is a Wednesday ")
            If dayNumber = 5 Then MsgBox(" The day on the " & Day & " " & Month & " " & Year & " is a Thursday ")
            If dayNumber = 6 Then MsgBox(" The day on the " & Day & " " & Month & " " & Year & " is a Friday ")

            End Sub

            'Exit Program

            Private Sub btnExit_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles btnExit.Click

            End

            End Sub
            End Class
            Sorry to say this. But its a complicated program to do a very small work. Try make something thats not such boring. People said that: everything is begining at zero, but i think thats an invalid statement.

            Comment

            • darni
              New Member
              • Jul 2007
              • 1

              #7
              hey hi i too faced the same problem..
              but i got the soln to get the day wen u type in the date with month and year....
              to be frank i got the logic from the agarwal's quantitative aptitude book..
              like u first take mod for the y-1... then subtract the result from the value of y-1
              ... u get two gr8 values a and b say... now perform this in both the values....
              1) divide b (say) by 4-say c

              2)b=b-c(now)

              3)this is to calculate the number of odd days in the year....o1=2*c+ b

              4)if the number is greater than 7 then perform mod 7 of ans and get hte result...

              5) do the same 1-4 procedure in a also...

              6)add both the odd num of days...

              7)perform mod 7 to get the exact odd days....

              8)count the number of days from day 1 of the year till ur i/p date....

              9)add it to odd num of days perform mod 7 and get the final answer....
              u cud even simplify it... refer to r.s.agarwal's aptitude book u can get it cleared....
              use switch case from 0-6 starting from sun to sat... and end up with ur desired result...
              all the best..!

              Comment

              • pureenhanoi
                New Member
                • Mar 2007
                • 175

                #8
                Originally posted by darni
                hey hi i too faced the same problem..
                but i got the soln to get the day wen u type in the date with month and year....
                to be frank i got the logic from the agarwal's quantitative aptitude book..
                like u first take mod for the y-1... then subtract the result from the value of y-1
                ... u get two gr8 values a and b say... now perform this in both the values....
                1) divide b (say) by 4-say c

                2)b=b-c(now)

                3)this is to calculate the number of odd days in the year....o1=2*c+ b

                4)if the number is greater than 7 then perform mod 7 of ans and get hte result...

                5) do the same 1-4 procedure in a also...

                6)add both the odd num of days...

                7)perform mod 7 to get the exact odd days....

                8)count the number of days from day 1 of the year till ur i/p date....

                9)add it to odd num of days perform mod 7 and get the final answer....
                u cud even simplify it... refer to r.s.agarwal's aptitude book u can get it cleared....
                use switch case from 0-6 starting from sun to sat... and end up with ur desired result...
                all the best..!
                Ouch!!!!!
                Nice idea

                Comment

                • Killer42
                  Recognized Expert Expert
                  • Oct 2006
                  • 8429

                  #9
                  We may have overlooked some simpler options here (unless the required range of 1600 to 2100 isn't acceptable to VB - I haven't checked that).

                  Here's a code module (VB6) containing two functions to convert a date to a weekday, and a day, month and year to a weekday. As you can see from the code, you don't need a special function, since they both just invoke the Format() function to do the work.

                  [CODE=vb]Option Explicit

                  Public Function Date2Weekday(By Val parmDate As Date) As String
                  Date2Weekday = Format$(parmDat e, "ddd")
                  End Function

                  Public Function DDMMYYYY2Weekda y(ByVal parmDD As Byte, ByVal parmMM As Byte, ByVal parmYYYY As Integer) As String
                  DDMMYYYY2Weekda y = Format$(DateSer ial(parmYYYY, parmMM, parmDD), "ddd")
                  End Function[/CODE]

                  Comment

                  Working...