Date Comparison

G

Guest

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(Calendar1.Value, "dd-mm-yy"))
Command1.Enabled = False
MsgBox CalendarDate1
End Sub


Private Sub Calendar2_Click()
CalendarDate2 = Trim(Format(Calendar2.Value, "dd-mm-yy"))
Command1.Enabled = False
Command3.SetFocus
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.
 
J

Jon Skeet [C# MVP]

Kurien Baker Fenn said:
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(Calendar1.Value, "dd-mm-yy"))
Command1.Enabled = False
MsgBox CalendarDate1
End Sub


Private Sub Calendar2_Click()
CalendarDate2 = Trim(Format(Calendar2.Value, "dd-mm-yy"))
Command1.Enabled = False
Command3.SetFocus
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.

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.
 
G

Guest

Jon Skeet said:
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.


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

Thanks in Advance
 
J

Jon Skeet [C# MVP]

Kurien Baker Fenn said:
My platform is visual basics and i have declared it as dim.

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.
 
C

Cor Ligthert

Kurien,

It is funny in the newsgroup microsoft.public.dotnet.languages.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)
Private Sub Calendar1_Click()
CalendarDate1 = Trim(Format(Calendar1.Value, "dd-mm-yy"))
Command1.Enabled = False
MsgBox CalendarDate1
End Sub
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
CalendarDate1 = Trim(Format(Calendar1.Value, "dd-mm-yy"))
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
 
C

Cor Ligthert

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

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

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top