how to compare 2 datetimepicker vb.net - vb.net

I'd like to compare 2 DateTimePicker's value
Dim dd1 As Date, dd2 As Date
Dim diff As Integer
dd1 = DateTimePicker1.Value
dd2 = DateTimePicker2.Value
diff = DateDiff("d", dd1, dd2)
If diff > 0 Then
MsgBox("datetimpicker1datetimepicker2")
End If
but it doesn't work could you help me please

Another way to compare would be to use the DateTime.Compare function. For a simple "Are they the same?" you could try:
If Not DateTime.Compare(dd1,dd2) = 0 then
'they are diffent
End If
Or if you want to be more specific then you might go with something like this:
If Not DateTime.Compare(dd1,dd2) = 0 then
'they are same
ElseIf DateTime.Compare(dd1,dd2) > 0 then
'dd1 is later than dd2
Else
'dd1 is prior to dd2
End If
You can see more about DateTime.Comare here on MSDN

If You are using DateDiff() then there are three case can be possible:-
1) DatePicker1 date is bigger than DatePicker2
2) DatePicker1 date is lower than DatePicker2
3) DatePicker1 date is equal to DatePicker2
To handle this, You have to use this
diff = DateDiff("d", dd1, dd2)
If diff > 0 Then
MsgBox("datetimpicker1 is greater than datetimepicker2")
Else If diff < 0 Then
MsgBox("datetimpicker1 is lesser than datetimepicker2")
Else
MsgBox("datetimpicker1 is equal to datetimepicker2")
End If

Related

How to compare only month and year? [VB]

Its quite simple, i just want to compare two dates using month and year, if the input date (mont and year only) are above or below that current date (month and year).
The problem is , when i compare two strings
Dim dDate as DateTime
If Not (DateTime.TryParse(txtBox.Text, dDate)) Then
MessageBox.Show("check date.")
Else
txtBox.Text = dDate.ToString("MM/yyyy")
end If
IF dDate.ToString("MM/yyyy") < DateTime.Now.ToString("MM/yyyy")
MessageBox.Show("Below") ' Problem: 03/2024 saying is below than 08/2019
Else
MessageBox.Show("Above")
End If
Any help?
UPDATE
I CHANGED THE CASE TO
If (dDate.Month AndAlso dDate.Year) < (DateTime.Now.Month AndAlso DateTime.Now.Year) Then
'input: 07/2019
'date expired
Else
'the problem is here
'saying 07/2019 is not < than 08/2019
End If
I'd avoid using strings.
Dim dDate As DateTime
If Not (DateTime.TryParse(txtBox.Text, dDate)) Then
'bad date
MessageBox.Show("check date.")
Else
Select Case dDate.Year
Case Is < DateTime.Now.Year
MessageBox.Show("Below")
Case Is > DateTime.Now.Year
MessageBox.Show("Above")
Case Else 'years are equal,check month
Select Case dDate.Month
Case Is < DateTime.Now.Month
MessageBox.Show("Below")
Case Is > DateTime.Now.Month
MessageBox.Show("Above")
Case Else 'equal months
MessageBox.Show("SAME") '????
End Select
End Select
End If
Using date values is probably best, but if string comparisons must be made.
Dim dDate as DateTime
If Not (DateTime.TryParse(txtBox.Text, dDate)) Then
MessageBox.Show("check date.")
Else
txtBox.Text = dDate.ToString("yyyyMM")
End If
If dDate.ToString("yyyyMM") < DateTime.Now.ToString("yyyyMM") Then
MessageBox.Show("Below")
ElseIf dDate.ToString("yyyyMM") > DateTime.Now.ToString("yyyyMM") Then
MessageBox.Show("Above")
Else
MessageBox.Show("Same")
End If
Thank you everyone, but the final solutions is made with
If (dDate.Year < DateTime.Now.Year Or (dDate.Year = DateTime.Now.Year And dDate.Month < DateTime.Now.Month)) Then
'something
Else
'something
End If
Convert the year and month to Integer values, which are easy to compare less-than or greater-than. For example, if you had two DateTimePicker controls on a form, named dtpDate1 and dptDate2, and a button btnTest:
Private Sub btnTest_Click(sender As Object, e As EventArgs) Handles btnTest.Click
Dim date1 As Integer = (dtpDate1.Value.Year * 100) + dtpDate1.Value.Month
Dim date2 As Integer = (dtpDate2.Value.Year * 100) + dtpDate2.Value.Month
MsgBox("Date1 = " & date1.ToString & vbCrLf & "Date2 = " & date2.ToString)
If date1 = date2 Then MsgBox("Equal!")
If date1 < date2 Then MsgBox("date1 is less than date2")
If date1 > date2 Then MsgBox("date1 is greater than date2")
End Sub
The dates are converted to integers in a YYYYMM format. This is a data warehouse technique to convert dates to integers for better query performance when date is used often in a WHERE clause.

if condition between from time and to time vb.net

i am trying to write a if condition in Vb.net where i would like to call a SQL procedure if the IF condition is satisfied.But the below condition which I am using is not working . Please let me know when i am wrong.
Dim dt As DateTime, hh As Integer, mm As Integer
dt = DateTime.Now
hh = dt.Hour
mm = dt.Minute
If ((hh > 00 or mm > 30) and (hh <= 9 or mm < 30 )) Then
End if
Need help if the if condition is correct .
I guess the condition is that the current time is between 0:30 and 9:30.
Dim now = DateTime.Now
Dim startTime = DateTime.Today.AddMinutes(30)
Dim endTime = DateTime.Today.AddHours(9).AddMinutes(30)
If now > startTime AndAlso now < endTime Then
End If
Note that you should use AndAlso and OrElse instead of And and Or

offset for weekend days using mod

I have a .NET console app that needs to run on the 1st, 5th, 10th, 15th, 20th, and 25th of each month. Depending on which date it runs, the app sends out a different type of email. I want to adjust the app so that if one of those dates is on the weekend, it runs the following Monday.
I believe this does what I want:
Dim adjustedDay As Integer = Day(Today)
If Today.ToString("ddd") = "Mon" And adjustedDay > 1 Then
If adjustedDay Mod 5 = 1 Then
adjustedDay -= 1
ElseIf adjustedDay Mod 5 = 2 Then
adjustedDay -= 2
End If
End If
Select Case adjustedDay
Case 1
...
Case 5
...
Case ...
...
End Select
So if today is Monday, 9/21/2015, I adjust to "20" and run that code. If Monday happens to land on the 1st, I don't adjust it.
is there a cleaner way to do this?
Your code won't work correctly if Monday falls on the 2nd or 3rd of the month. (ie. 2 mod 5 = 2, so adjustedDay = 0)
The other tricky part is that the app needs to run on the 1st, but 1 mod 5 = 1 which is different to all the other cases where it is 0.
Something like this should work:
'Returns 0 for non-email days, otherwise returns 1,5,10,15,20,25
Private Function GetEmailDay(ByVal currentDate as Date) As Integer
If currentDate.DayOfWeek = DayOfWeek.Saturday OrElse
currentDate.DayOfWeek = DayOfWeek.Sunday Then
Return 0
End If
Dim day As Integer = currentDate.Day
Dim emailDays As New List(Of Integer)( {1,5,10,15,20,25} )
If emailDays.Contains(day) Then
Return day
End If
If currentDate.DayOfWeek = DayOfWeek.Monday Then
If emailDays.Contains(day-1) Then 'check Sunday
Return day-1
End If
If emailDays.Contains(day-2) Then 'check Saturday
Return day-2
End If
End If
Return 0
End Function

Finding if a date is in the Future or in the Past

'checks to see if date is within x days of the inputted date
Function isFutureDate(x) As Boolean
Dim daysFuture As Integer
Dim futureDate As Date
daysFuture = Sheet1.Range("e1").Value - 1
'we have to add one to not count today
futureDate = WorksheetFunction.WorkDay(Date, daysFuture)
If x >= Date And x < futureDate Then
isFutureDate = True
Else
isFutureDate = False
End If
End Function
'checks to see if date is in the past x days
Function isPastDate(x) As Boolean
Dim BDate As Date
Dim y As Date
Dim daysPast As Integer
'subtract one to not count today
daysPast = Sheet1.Range("E1").Value - 1 'subtract one to not count today
BDate = WorksheetFunction.WorkDay(Date, -1 * daysPast)
If x < Date And x > BDate Then
isPastDate = True
Else
isPastDate = False
End If
End Function
These are the two functions I have currently. x is passed as a Date. When I step through the program, I notice an error with BDate in the isPastDate function. In my file, I have a cell where the user enters how many days in the future they would like to see entries for. I think this is where my main problem is. When I check the value of daysFuture or daysPast I get 0 while the user entered value is clearly 7.
'checks to see if date is within x days of the inputted date
Function isFutureDate(x As Date) As Boolean
Dim xDays As Integer
Dim xFuture As Integer
xDays = Sheet1.Range("E1").Value
xFuture = DateDiff("d", Date, x)
If xFuture > xDays Then
isFutureDate = True
Else
isFutureDate = False
End If
End Function
'checks to see if date is in the past x days
Function isPastDate(x As Date) As Boolean
Dim xDays As Integer
Dim xPast As Integer
xDays = Sheet1.Range("E1").Value
xPast = DateDiff("d", Date, x)
If xPast < xDays Then
isPastDate = True
Else
isPastDate = False
End If
End Function

how to check what the first character of a string is in vb

I have the following code, which reads the date and time from some DateTimePickers in VB.
I need to be able to determine if the first value is a 0 or a 1, (eg 09:12... or 12:13...) and if it starts with a 0 to remove that character from the string.
this is what i have so far, but it takes the first character regardless.
DateFrom = Form1.DateTimePickerFrom.Value.ToString
DateTo = Form1.DateTimePickerTo.Value.ToString
VarTimeFrom = Form1.HourTimePickerFrom.Value.ToString
VarTimeTo = Form1.HourTimePickerTo.Value.ToString
Dim DateFromManipulated = Left(DateFrom, 10)
Dim DateToManipulated = Left(DateTo, 10)
Dim TimeFromManipulated = Right(VarTimeFrom, 9)
Dim TimeToManipulated = Right(VarTimeTo, 9)
If Left(DateFromManipulated, 1) = 0 Then
TimeFromMan = TimeFromManipulated.Remove(0, 1)
Else
TimeFromMan = TimeFromManipulated
End If
If Left(TimeFromManipulated, 1) = 0 Then
TimeToMan = TimeToManipulated.Remove(0, 1)
Else
TimeToMan = TimeToManipulated
End If
Console.WriteLine(DateFromManipulated)
Console.WriteLine(TimeToMan)
Console.WriteLine(TimeFromManipulated)
Console.WriteLine(TimeFromMan)
Console.WriteLine(DateToManipulated)
Console.WriteLine(TimeToManipulated)
I get the following:
09/11/2012
1:36:00
06:36:00
6:36:00
08/01/2013
11:36:00
Thanks in advance!
Mike
A string in VB.NET won't compare as equal to an integer. You could just reference character zero, though:
If DateFromManipulated(0) = "0"c Then DateFromManipulated = DateFromManipulated.Substring(1)
... however, you should be just formatting your date the way you want it to begin with:
Dim dateFrom As String = DateTimePickerFrom.Value.ToString("M/dd/yyyy H:mm:ss")
... for example. (M doesn't have a leading zero, as opposed to MM; same with H.) You can find all the format strings here: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx