Link to home
Start Free TrialLog in
Avatar of José Perez
José PerezFlag for Chile

asked on

VB: Convert 2 dates to specific format

Hi,
I have the following code that is not working:
If objKey.PaymentDate.Value.ToShortDateString() = CStr(Fecha) Then

Open in new window

Where:
"objKey.PaymentDate.Value.ToShortDateString()" comes from a Database in the format "dd-MM-yyyy"
And:
"CStr(Fecha)" is the date gotten from a Calendar control in the format "MM/dd/yyyy"

How can I do to make them equivalent so the program runs into the IF statement? (Obviusly without modifying the database)
Thanks.
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of José Perez

ASKER

Not working. I did this:
objKey.PaymentDate.Value.ToString("MM-dd-yyyy") = Format(CStr(Fecha), "MM-dd-yyyy") Then

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
To make sure they always be in the same format, independent of the machine I run this app.
If Fecha is a date value then just compare the two date values without any conversion
if fecha.Date = objKey.PaymentDate.Value.Date then
....
end if
Agree with Andy, Why you are even modifying your dates to strings and then comparing. We should always use dates for date kind of comparison. Engine will automatically handle these kinds of things.

Use this directly-

If objKey.PaymentDate = Fecha
--------

Hope it helps!
@Pawan: That's because "objKey.PaymentDate" comes with time ("objKey.PaymentDate.Value = #12/19/2016 6:36:26 PM#").
Is objKey.PaymentDate coming from database ?
@Pawan: YES.
Then use this in your DB

CAST(yourdatecolumn as DATE)

Hope it helps!
I cannot chnage DB. I can change VB source code only.
I am not asking to change the database , I am asking you to change the query. How are you getting - Stored procedure or , Embedded query ? If possible change there.

Also if not then try this - yourdate.ToString("d", yourdatecolumn)
Erm.
if fecha.Date = objKey.PaymentDate.Value.Date then
Just uses the date component, the time value is ignored.
You could also use DateDiff:

If DateDiff("d", objKey.PaymentDate.Value, Fecha) = 0 Then

Open in new window

/gustav
Finally I did this, and worked as expected.
If objKey.PaymentDate.Value.ToString("MM-dd-yyyy") = Format(Fecha, "MM-dd-yyyy") Then

Open in new window

Thanks to all.
Almost exactly. Just made 1 minor change.
Initially i did the 'code' part and it did not work. Finally, I tried with the code in the 'Comment' (instead the code in [code][/code] part) and it worked.
Thanks.
It will work, but it is suboptimal - it is a deroute to cast to string for the comparison.

/gustav
It really looks like you are starting with two datetime objects.  What I suggested works, is machine independent, is country independent and efficient.  Converting to strings and then doing a string comparison will work but not efficiently.

ps.  @Gustav.  DateDiff won't work for certain inputs.  It depends on just what the source values are.
@AndyAinscow Why it is not efficient? You mean in terms of 'response time' or effectiveness?
String comparisons are performed character by character.  Date (and DateTime) are basically numbers in disguise.  Comparing numbers is more efficient than working along a string comparing one character after the next.  Let alone having to convert the number into a specially formatted string in the first place.
DateDiff accepts - even for seconds - parameter values from MinValue to MaxValue and vice-versa, so what "particular" values will it not work for, please?

/gustav
>>If DateDiff("d", objKey.PaymentDate.Value, Fecha) = 0 Then

DateDiff("d", dt1, dt2)  returns the number of days (periods of 24 hours) between the dt1 and dt2.  It does NOT check if they are on the same calendar day.  eg.  "15.2.17 10:00:00" and "16.2.17 09:59:59" will return 0 as will "15.2.17 10:00:00" and "14.2.17 10:00:01" but they are not on the same day in either case.
Oh, you are right Andy. To be honest, I only do VBA and C# ... and only play with VB.NET for the fun. So I thought DateDiff would behave as in VBA where it returns the difference in calendar days etc. ignoring the smaller parts. But, as I can see, that is not the case in VB.NET; here it is the difference in full intervals. I really wonder why, as you have the TimeSpan for that sort of calculations.

Thanks for the head-up.

/gustav
No problems.  I don't understand Microsoft - they make a 'common language' with a number of different coding interfaces (VB.net, C#, C++.net...) and then have functionality available in only one of them which is actually covered by another class available to all.

<OK, I know one can add a reference to use it in the other languages but...>


ps.  I had to check myself first how it worked, my first thoughts your suggestion was a viable method