Date Comparison

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kurien Baker Fenn

    #1

    Date Comparison

    This is the code that compares the two dates

    If (CalendarDate1 >= CalendarDate2) Then
    MsgBox "Please enter the correct date range", vbOKOnly + vbExclamation,
    "SASMonitor "
    Exit Sub
    End If


    The below code is where i get the date from the user.i have two calendar
    components in my form.that's why two calendar

    Private Sub Calendar1_Click ()
    CalendarDate1 = Trim(Format(Cal endar1.Value, "dd-mm-yy"))
    Command1.Enable d = False
    MsgBox CalendarDate1
    End Sub


    Private Sub Calendar2_Click ()
    CalendarDate2 = Trim(Format(Cal endar2.Value, "dd-mm-yy"))
    Command1.Enable d = False
    Command3.SetFoc us
    MsgBox CalendarDate2
    End Sub


    'when i do this i am not able to get the correct comparison.the case is when
    i select 10th may 2004 from the first calendar and 1st june 2004 from the
    second calendar,it says that 10th may 2004 is greater.pls help me.
    Thanks in advance.
  • Jon Skeet [C# MVP]

    #2
    Re: Date Comparison

    Kurien Baker Fenn <KurienBakerFen n@discussions.m icrosoft.com> wrote:[color=blue]
    > This is the code that compares the two dates
    >
    > If (CalendarDate1 >= CalendarDate2) Then
    > MsgBox "Please enter the correct date range", vbOKOnly + vbExclamation,
    > "SASMonitor "
    > Exit Sub
    > End If
    >
    >
    > The below code is where i get the date from the user.i have two calendar
    > components in my form.that's why two calendar
    >
    > Private Sub Calendar1_Click ()
    > CalendarDate1 = Trim(Format(Cal endar1.Value, "dd-mm-yy"))
    > Command1.Enable d = False
    > MsgBox CalendarDate1
    > End Sub
    >
    >
    > Private Sub Calendar2_Click ()
    > CalendarDate2 = Trim(Format(Cal endar2.Value, "dd-mm-yy"))
    > Command1.Enable d = False
    > Command3.SetFoc us
    > MsgBox CalendarDate2
    > End Sub
    >
    >
    > 'when i do this i am not able to get the correct comparison.the case is when
    > i select 10th may 2004 from the first calendar and 1st june 2004 from the
    > second calendar,it says that 10th may 2004 is greater.pls help me.[/color]

    What are CalendarDate1 and CalendarDate2 declared as? You're using them
    as if they're dates at the top, but as strings lower down. You should
    get them as DateTimes before doing comparisons.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Kurien Baker Fenn

      #3
      Re: Date Comparison



      "Jon Skeet [C# MVP]" wrote:
      [color=blue]
      > Kurien Baker Fenn <KurienBakerFen n@discussions.m icrosoft.com> wrote:[color=green]
      > > This is the code that compares the two dates
      > >
      > > If (CalendarDate1 >= CalendarDate2) Then
      > > MsgBox "Please enter the correct date range", vbOKOnly + vbExclamation,
      > > "SASMonitor "
      > > Exit Sub
      > > End If
      > >
      > >
      > > The below code is where i get the date from the user.i have two calendar
      > > components in my form.that's why two calendar
      > >
      > > Private Sub Calendar1_Click ()
      > > CalendarDate1 = Trim(Format(Cal endar1.Value, "dd-mm-yy"))
      > > Command1.Enable d = False
      > > MsgBox CalendarDate1
      > > End Sub
      > >
      > >
      > > Private Sub Calendar2_Click ()
      > > CalendarDate2 = Trim(Format(Cal endar2.Value, "dd-mm-yy"))
      > > Command1.Enable d = False
      > > Command3.SetFoc us
      > > MsgBox CalendarDate2
      > > End Sub
      > >
      > >
      > > 'when i do this i am not able to get the correct comparison.the case is when
      > > i select 10th may 2004 from the first calendar and 1st june 2004 from the
      > > second calendar,it says that 10th may 2004 is greater.pls help me.[/color]
      >
      > What are CalendarDate1 and CalendarDate2 declared as? You're using them
      > as if they're dates at the top, but as strings lower down. You should
      > get them as DateTimes before doing comparisons.
      >
      > --
      > Jon Skeet - <skeet@pobox.co m>
      > http://www.pobox.com/~skeet
      > If replying to the group, please do not mail me too
      >[/color]


      i declared Calendardate1 and calendardate2 as dim.so what should i do to
      compare.Please help

      Thanks in Advance

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Date Comparison

        Kurien Baker Fenn <KurienBakerFen n@discussions.m icrosoft.com> wrote:[color=blue]
        > i declared Calendardate1 and calendardate2 as dim.so what should i do to
        > compare.Please help[/color]

        What do you mean "as dim"?

        Could you post a short but complete program which demonstrates the
        problem?

        See http://www.pobox.com/~skeet/csharp/complete.html for details of
        what I mean by that.

        Do you have option strict on, by the way?

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • Kurien Baker Fenn

          #5
          Re: Date Comparison



          "Jon Skeet [C# MVP]" wrote:
          [color=blue]
          > Kurien Baker Fenn <KurienBakerFen n@discussions.m icrosoft.com> wrote:[color=green]
          > > i declared Calendardate1 and calendardate2 as dim.so what should i do to
          > > compare.Please help[/color]
          >
          > What do you mean "as dim"?
          >
          > Could you post a short but complete program which demonstrates the
          > problem?
          >
          > See http://www.pobox.com/~skeet/csharp/complete.html for details of
          > what I mean by that.
          >
          > Do you have option strict on, by the way?
          >
          > --
          > Jon Skeet - <skeet@pobox.co m>
          > http://www.pobox.com/~skeet
          > If replying to the group, please do not mail me too
          >[/color]


          My platform is visual basics and i have declared it as dim.

          Comment

          • Kurien Baker Fenn

            #6
            Re: Date Comparison



            "Jon Skeet [C# MVP]" wrote:
            [color=blue]
            > Kurien Baker Fenn <KurienBakerFen n@discussions.m icrosoft.com> wrote:[color=green]
            > > This is the code that compares the two dates
            > >
            > > If (CalendarDate1 >= CalendarDate2) Then
            > > MsgBox "Please enter the correct date range", vbOKOnly + vbExclamation,
            > > "SASMonitor "
            > > Exit Sub
            > > End If
            > >
            > >
            > > The below code is where i get the date from the user.i have two calendar
            > > components in my form.that's why two calendar
            > >
            > > Private Sub Calendar1_Click ()
            > > CalendarDate1 = Trim(Format(Cal endar1.Value, "dd-mm-yy"))
            > > Command1.Enable d = False
            > > MsgBox CalendarDate1
            > > End Sub
            > >
            > >
            > > Private Sub Calendar2_Click ()
            > > CalendarDate2 = Trim(Format(Cal endar2.Value, "dd-mm-yy"))
            > > Command1.Enable d = False
            > > Command3.SetFoc us
            > > MsgBox CalendarDate2
            > > End Sub
            > >
            > >
            > > 'when i do this i am not able to get the correct comparison.the case is when
            > > i select 10th may 2004 from the first calendar and 1st june 2004 from the
            > > second calendar,it says that 10th may 2004 is greater.pls help me.[/color]
            >
            > What are CalendarDate1 and CalendarDate2 declared as? You're using them
            > as if they're dates at the top, but as strings lower down. You should
            > get them as DateTimes before doing comparisons.
            >
            > --
            > Jon Skeet - <skeet@pobox.co m>
            > http://www.pobox.com/~skeet
            > If replying to the group, please do not mail me too
            >[/color]


            i declared Calendardate1 and calendardate2 as dim.so what should i do to
            compare.Please help

            Thanks in Advance

            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: Date Comparison

              Kurien Baker Fenn <KurienBakerFen n@discussions.m icrosoft.com> wrote:[color=blue]
              > My platform is visual basics and i have declared it as dim.[/color]

              No, dim is what you use *to* declare it - have you not specified what
              actual type it should be? eg

              Dim CalendarDate1 as DateTime

              If you don't have option strict on, I strongly recommend that you put
              it on.

              Again, could you post a short but complete program which demonstrates
              the problem?

              See http://www.pobox.com/~skeet/csharp/complete.html for details of
              what I mean by that.

              --
              Jon Skeet - <skeet@pobox.co m>
              Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

              If replying to the group, please do not mail me too

              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: Date Comparison

                Kurien Baker Fenn <KurienBakerFen n@discussions.m icrosoft.com> wrote:[color=blue]
                > i declared Calendardate1 and calendardate2 as dim.so what should i do to
                > compare.Please help[/color]

                What do you mean "as dim"?

                Could you post a short but complete program which demonstrates the
                problem?

                See http://www.pobox.com/~skeet/csharp/complete.html for details of
                what I mean by that.

                Do you have option strict on, by the way?

                --
                Jon Skeet - <skeet@pobox.co m>
                Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                If replying to the group, please do not mail me too

                Comment

                • Cor Ligthert

                  #9
                  Re: Date Comparison

                  Kurien,

                  It is funny in the newsgroup microsoft.publi c.dotnet.langua ges.vb there was
                  someone who had a kind same problem, it was seen directy in the code. (That
                  newsgroup is special for VBNet questions)

                  (I assume that Calendar1 is a datetimepicker because that has a value
                  property)
                  [color=blue]
                  >
                  > Private Sub Calendar1_Click ()
                  > CalendarDate1 = Trim(Format(Cal endar1.Value, "dd-mm-yy"))
                  > Command1.Enable d = False
                  > MsgBox CalendarDate1
                  > End Sub[/color]
                  That mm tells minutes so that will certainly give a problem months are MM,
                  however better is in my opinion as I said in my other message to keep it
                  with the dateTime and use that for comparasing and not the string.

                  Which means that you can use the Calender1.value and the Calender2.value for
                  comparing.

                  In VBNet you can do this by the way as simple as[color=blue]
                  > CalendarDate1 = Trim(Format(Cal endar1.Value, "dd-mm-yy"))[/color]
                  dim CalendarDate1 as string = Cstr(Calendar1. Value).Format(" dd-MM-yy")
                  or even more easy because the datetimepicker has this already in it
                  dim CalendarDate1 as string = Calendar1.Text. Format("dd-MM=yy")

                  I hope this helps?

                  Cor


                  Comment

                  • Kurien Baker Fenn

                    #10
                    Re: Date Comparison



                    "Jon Skeet [C# MVP]" wrote:
                    [color=blue]
                    > Kurien Baker Fenn <KurienBakerFen n@discussions.m icrosoft.com> wrote:[color=green]
                    > > i declared Calendardate1 and calendardate2 as dim.so what should i do to
                    > > compare.Please help[/color]
                    >
                    > What do you mean "as dim"?
                    >
                    > Could you post a short but complete program which demonstrates the
                    > problem?
                    >
                    > See http://www.pobox.com/~skeet/csharp/complete.html for details of
                    > what I mean by that.
                    >
                    > Do you have option strict on, by the way?
                    >
                    > --
                    > Jon Skeet - <skeet@pobox.co m>
                    > http://www.pobox.com/~skeet
                    > If replying to the group, please do not mail me too
                    >[/color]


                    My platform is visual basics and i have declared it as dim.

                    Comment

                    • Jon Skeet [C# MVP]

                      #11
                      Re: Date Comparison

                      Kurien Baker Fenn <KurienBakerFen n@discussions.m icrosoft.com> wrote:[color=blue]
                      > My platform is visual basics and i have declared it as dim.[/color]

                      No, dim is what you use *to* declare it - have you not specified what
                      actual type it should be? eg

                      Dim CalendarDate1 as DateTime

                      If you don't have option strict on, I strongly recommend that you put
                      it on.

                      Again, could you post a short but complete program which demonstrates
                      the problem?

                      See http://www.pobox.com/~skeet/csharp/complete.html for details of
                      what I mean by that.

                      --
                      Jon Skeet - <skeet@pobox.co m>
                      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                      If replying to the group, please do not mail me too

                      Comment

                      • Cor Ligthert

                        #12
                        Re: Date Comparison

                        Kurien,

                        It is funny in the newsgroup microsoft.publi c.dotnet.langua ges.vb there was
                        someone who had a kind same problem, it was seen directy in the code. (That
                        newsgroup is special for VBNet questions)

                        (I assume that Calendar1 is a datetimepicker because that has a value
                        property)
                        [color=blue]
                        >
                        > Private Sub Calendar1_Click ()
                        > CalendarDate1 = Trim(Format(Cal endar1.Value, "dd-mm-yy"))
                        > Command1.Enable d = False
                        > MsgBox CalendarDate1
                        > End Sub[/color]
                        That mm tells minutes so that will certainly give a problem months are MM,
                        however better is in my opinion as I said in my other message to keep it
                        with the dateTime and use that for comparasing and not the string.

                        Which means that you can use the Calender1.value and the Calender2.value for
                        comparing.

                        In VBNet you can do this by the way as simple as[color=blue]
                        > CalendarDate1 = Trim(Format(Cal endar1.Value, "dd-mm-yy"))[/color]
                        dim CalendarDate1 as string = Cstr(Calendar1. Value).Format(" dd-MM-yy")
                        or even more easy because the datetimepicker has this already in it
                        dim CalendarDate1 as string = Calendar1.Text. Format("dd-MM=yy")

                        I hope this helps?

                        Cor


                        Comment

                        • Cor Ligthert

                          #13
                          Re: Date Comparison

                          Correction thanks to Jay
                          [color=blue]
                          > dim CalendarDate1 as string = Cstr(Calendar1. Value).Format(" dd-MM-yy")[/color]
                          Dim CalendarDate1 As String = Calendar1.Value .ToString("dd-MM-yy")
                          [color=blue]
                          > dim CalendarDate1 as string = Calendar1.Text. Format("dd-MM=yy")[/color]
                          Dim CalendarDate1 As String = CDate(Calendar1 .Text).ToString ("dd-MM-yy")

                          I should not have done this without IDE, to quick.

                          Cor


                          Comment

                          • Cor Ligthert

                            #14
                            Re: Date Comparison

                            Correction thanks to Jay
                            [color=blue]
                            > dim CalendarDate1 as string = Cstr(Calendar1. Value).Format(" dd-MM-yy")[/color]
                            Dim CalendarDate1 As String = Calendar1.Value .ToString("dd-MM-yy")
                            [color=blue]
                            > dim CalendarDate1 as string = Calendar1.Text. Format("dd-MM=yy")[/color]
                            Dim CalendarDate1 As String = CDate(Calendar1 .Text).ToString ("dd-MM-yy")

                            I should not have done this without IDE, to quick.

                            Cor


                            Comment

                            Working...