cancel
Showing results for 
Search instead for 
Did you mean: 

COMPARE DATES

Former Member
0 Kudos

Hi....

I have created a form using screen painter. In that i have assigned a textbox for date as date field. The user can enter the current date or the date which is lesser than current date while adding or updating. It should not allow the user, to enter the date which is greater than current date.

Regards

Roseline.B

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi,

Follow what Satish and Kevin said, and just in case if ur stuck in the comparision of the dates.. Check the following links.

http://stackoverflow.com/questions/618878/vb-net-compare-just-the-date-part-and-not-the-time-of-two-...

http://visualbasic.ittoolbox.com/groups/technical-functional/vb-dotnet-l/comparing-dates-in-vb-net-1...

http://www.dreamincode.net/forums/showtopic63297.htm

U can find even more samples in google.

Happy Coding..

Vasu Natari.

Former Member
0 Kudos

Hi Roseline,

In the Lost_Focus Event, for that particular field, you can make a check if the date is greater than the current date and if so, you can get the focus back to the date field and give a msg to the user asking him to enter the current date or a previous date.

Hope this helps.

Regards,

Satish. B.

Former Member
0 Kudos

I agree with satish, but when any body change postiing period there is may not work So take Lost focus event but dont compare with current date but compare with Current Posting Date

Thanks

Kevin

Edited by: Kevin Shah on Sep 10, 2009 1:31 PM

Former Member
0 Kudos

Hi......

Here the event is not a matter. I just need the conditions how they can be compared.Because in my form, It is just comparing the day part alone not the month and year part.

Regards

Roseline.B

Former Member
0 Kudos

Hi Roseline,

This is how you subtract dates.

DateTime oDate;
                DateTime oDate1;
                Int16 DateDifference =  oDate.Subtract(oDate1);

you get the DateDifference integer and if that is positive, then, it means the date is exceeding and you can delete the value and prompt the user to enter the correct date.

Hope this helps.

Regards,

Satish.

Former Member
0 Kudos

hi...

This is correct when we need to find the difference between dates. But i need to compare the dates and need to show the message when it is greater than current date

Thank you!

Regards

Roseline.B

Former Member
0 Kudos

yeah Roseline,

You are correct, so in the above code snippet of mine, one of the dates will be your current date, preferably, oDate. So, once you get the datedifference integer as say 3, then, it means that, the date entered is 3 days ahead of the current date.

In that case, you need to handle the validation such that, you explicitly delete the entered value and ask the user to enter a proper date.

I guess your question is answered here.

Regards,

Satish.

Former Member
0 Kudos

Hi...

I have tried your coding.. But got a error in

datediff=odate.subtract(odate1)

as

"Value of 'System.TimeSpan' cannot be converted to integer"

Regards

Roseline.B

Former Member
0 Kudos

Fine Roseline, then, what I would suggest you is, dont take datediff, take the rest of line in a message box and see in what datatype it comes, as

System.Windows.Forms.MessageBox.Show(oDate.Subtract(oDate1));

or MsgBox(oDate.Subtract(oDate1))

if your code is C# or VB.Net respectively.

Accordingly, you can assign it in integer or whatever and get it done.

Regards,

Satish.

Former Member
0 Kudos

I am doing in vb.net. In the message box itself it is showing error. I am getting the date value in the text box. The value is showing as string like this '20090915' if the date is '15/09/2009'. So I put

ODate1=otext.value

In that It shows a error as

"Conversion from '15/09/2009' to string is not valid