I can't work out why this VB.net code is not working..
What I am trying to do is if value1 > value2
then show a messagebox saying expired else show a messagebox saying not expired.
If "4-3-13 10:54:22" > "15-3-13 12:23:30" Then
MsgBox("Expired")
Else
MsgBox("Not Expired")
End If
everytime it comes up saying expired even knowing it shouldn't.
When I change it from 15-3-13 12:23:30
to 1-3-13 12:23:30
it still say expired.
If I change my code to be:
If "4-3-13 10:54:22" < "15-3-13 12:23:30" Then
MsgBox("Not Expired")
Else
MsgBox("Expired")
End If
It still returns wrong.
How do I make it so that:
DATE1 = 4-3-13 10:54:22
DATE2 = 15-3-13 12:23:30
IF DATE1 > DATE2 THEN
Expired
else
Not Expired
Should return 'Not expired'
Anyone able to help.. I can't work it ?
T he Select specific cells utility of Kutools for Excel can help you quickly find the dates greater than another date in Excel. Kutools for Excel : with more than 300 handy Excel add-ins, free to try with no limitation in 30 days. 1. Select the range with dates you want to compare, and then click Kutools > Select > Select Specific Cells.
GENERIC FORMULA. =IF (date>greater_date,value_if_true,value_if_false) ARGUMENTS. date: A date that you want to test against another date to identify if it's greater than this date. greater_date: A date that another date is tested against. value_if_true: Value to be returned if the date is greater than the greater_date.
Using the IF Function with the TODAY Function If you want to compare a date with the current date, you can use the IF function with the TODAY function in the logical test. Let’s say you have a date in cell A2 and you want cell B2 to display the value “done” if it is a date before today’s date.
Which one to use depends on exactly what your needs are. Example 1. Excel DATEDIF formula to calculate date difference in days Supposing you have the start date in cell A2 and the end date in cell B2 and you want Excel to return the date difference in days. A simple DATEDIF formula works just fine:
"4-3-13 10:54:22" > "15-3-13 12:23:30"
'This condition states that you are comaparring strings not date
In order to get the result as you've expected, do like this,
cdate("4-3-13 10:54:22") > cdate("15-3-13 12:23:30")
'Convert the strings into date and then compare it.
These date constants will also do as you expect, and won't be subject to the locale when the program is run:
#3/4/2013 10:54:22# > #3/15/2013 12:23:30#
Just remember you need to use US Date Format for the constants.
Try this:
dim date1 as DateTime = DateTime.ParseExact("4-3-13 10:54:22", "MM-dd-yy HH:mm:ss")
dim date2 as DateTime = DateTime.ParseExact("15-3-13 12:23:30", "MM-dd-yy HH:mm:ss")
if date1 > date2 then
MsgBox("Expired")
else
MsgBox("Not Expired")
end if
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With