subtract from one date another - vb.net

Good day. There is the following code that subtracts from one date another:
Dim dt As New DateTime(2021, 8, 19)
Dim dt2 As New DateTime(2022, 8, 29)
MsgBox("Days Remaining : " & (dt2 - dt).Days)
tell me how to subtract format dates:
19.08.2021 14:07:16 - 24.08.2021 08:24:01
throwing away the time. I'm only interested in how to subtract dates of this format.
Date and time are entered in text boxes

You would get the Date property (documentation) of the DateTime values. Also, I would probably recommend using DateTime.Subtract (documentation) which would return a TimeSpan value.
Take a look at this example:
Dim dt = New DateTime(2021, 8, 19)
Dim dt2 = New DateTime(2022, 8, 29)
Dim daysLeft = dt2.Subtract(dt).Days
Example: https://dotnetfiddle.net/LMuWtu

You have to parse the formatted Dates then use .Date to keep only the Date part and throw away the time. Then you can substract and use TotalDays:
Dim input1 As String = "19.08.2021 14:07:16"
Dim input2 As String = "19.08.2021 14:07:16"
Dim d1 As DateTime = DateTime.Parse(input1).Date
Dim d2 As DateTime = DateTime.Parse(input1).Date
Dim days As Single = (d1 - d2).TotalDays
The formatted dates should match your Culture format. You can also use Parse overload to use another Culture : https://learn.microsoft.com/fr-fr/dotnet/api/system.datetime.parse?view=net-5.0#System_DateTime_Parse_System_String_System_IFormatProvider_

Related

How to Convert Integer to Date?

Is it possible to convert integer to date? I am using a datagridview where users can type a combinations of number (e.g 07191993) when in editmode then after the user is done editing, the program should format it in date (07-19-1993).
To get a date from the number inputed by user :
Dim theDate = DateTime.ParseExact (input, "MMddyyyy", CultureInfo.InvariantCulture) ' or some other specific CultureInfo / FormatProvider)
To display the date as wanted (if given FormatProvider doesn't do it by itself) :
Dim repr = theDate.ToString ("MM-dd-yyyy")
Create an event for CellEndEdit of datagridview and put in this code
Dim n As Integer = 'Datagridview cell value'
Dim mon As Integer = Convert.ToInt32(n.ToString.Substring(0, 1))
Dim dt As Integer = Convert.ToInt32(n.ToString.Substring(1, 2))
Dim yr As Integer = Convert.ToInt32(n.ToString.Substring(3, 4))
Dim dt1 As New DateTime(yr, mon, dt)
hope this helps.

VB.NET - How do I find difference in dates in MM/DD format?

If I have two dates
Dim date1 As String = 01/05
Dim date2 As String = 01/07
Dim date As Integer
Date = 2
How do I go about making sure that 2 handles month changes and just general dates in this format?
You would parse the strings into DateTime values, then you can subtract them to get a TimeSpan value that is the difference. Use the Days property to get the whole days from the value:
Dim date1 As String = "01/05"
Dim date2 As String = "01/07"
Dim d1 = DateTime.ParseExact(date1, "MM'/'dd", CultureInfo.InvariantCulture)
Dim d2 = DateTime.ParseExact(date2, "MM'/'dd", CultureInfo.InvariantCulture)
Dim date As Integer = (d2 - d1).Days

Check if date is between or equal to two other dates

I'm trying to check if a date is between two other dates. The two other dates are coming from two DatePickers formatted as dd/mm/yyyy. My code below works but if my searching date is i.e. equal to the from date I get a "Not between" message. If I search for date 17/05/2013 and I set the ranges to bet from: 17/05/2013 and to: 17/05/2013 I want to get a "Between" message. Any ideas?
Dim str As String = "srt_inlbp_20130517"
Dim sString As String
sString = str.Substring(str.Length - 8)
Dim dTableDate As Date = Date.ParseExact(str.Substring(str.Length - 8), "yyyyMMdd", Nothing)
Label9.Text = dTableDate
If ((dTableDate >= dFromDate.Value) And (dTableDate <= dToDate.Value)) Then
MsgBox("between")
Else
MsgBox("not between")
End If
I have rewritten your code in this testable format:
Dim str As String = "srt_inlbp_20130517"
Dim dTableDate As Date = _
Date.ParseExact(str.Substring(str.Length - 8), "yyyyMMdd", Nothing)
Dim dFromDate As New DateTime(2013, 5, 17)
Dim dToDate As New DateTime(2013, 6, 17)
If ((dTableDate >= dFromDate) And (dTableDate <= dToDate)) Then
Console.WriteLine("between")
Else
Console.WriteLine("not between")
End If
I get the result "between" so I can only suspect that your dFromDate.Value and/or dToDate.Value are not as you expect them to be. Can you check this please?
Try to write your comparators like you are reading a number line (e.g. always use a less-than sign).
Dim questionableDate As Date = New Date(2013, 05, 17)
Dim fromDate As Date = New Date(2013, 05, 17)
Dim toDate As Date = New Date(2013, 05, 17)
If (fromDate <= questionableDate) AndAlso (questionableDate <= toDate) Then
MsgBox("between")
Else
MsgBox("not between")
End If
You'll see that by using the number line approach you can easily determine what the code is doing. The from date and to date are at the ends with the questionable date sandwiched in the middle.
Note
If the code above does not wield the results you expect once you integrate it, then you will need to set some breakpoints and step through your code to see exactly what values your variables have.

If date is (certain date) then

How would I make my application do an event when there is a certain date? I can't seem to find something that works. Year doesn't matter, just day and month is all I need.
Since day and month is all you want to match on:
Dim myDate As Date = Date.Now
Dim someMonth As Integer = 4
Dim someDay As Integer = 13
If myDate.Month = someMonth AndAlso myDate.Day = someDay Then
... month and day are matched
End If
Use IsDate....
If IsDate(MyDate) Then.....
Update various ways of defining date
'DateTime constructor: parameters year, month, day, hour, min, sec
Dim MyDate1 As New Date(2012, 12, 10, 12, 0, 0)
'initializes a new DateTime value
Dim MyDate2 As Date = #12/10/2012 12:00:52 AM#
'using properties
Dim MyDate3 As Date = Date.Now
Dim MyDate4 As Date = Date.UtcNow
Dim MyDate5 As Date = Date.Today
Try something like this:
Dim thisDate As Date = New Date(2014, 4, 13) ' Assuming this is provided
Dim certainDate As Date = New Date(2013, 4, 13) ' Assuming this is provided
If thisDate = New Date(thisDate.Date.Year, certainDate.Month, certainDate.Day) Then
Debug.Print("dates equal excluding year")
Else
Debug.Print("dates UNequal excluding year")
End If
Rememebr, this will always create a new date object for comparison but it works.
Hope it helps!

"Argument 'DateValue' cannot be converted to type 'Date'"

All
I'm a beginner working in VB 2010. I'm working on a reminder tied into Task Scheduler until I get to working with "open at a this time" kind of thing. Anyway, I want it read dated lines from a text file until the date is more than 7 days from today. It will auto read any non-dated line until it reaches + 7 days. Also I would like it to work with any date format mdy, dmy or ymd.
If it makes a difference in Control Panel>Region my Home Country = United States and the Short Date = yyyy-MM-DD.
Using "Copy execption detail to the clipboard" I got the following:
System.InvalidCastException was unhandled
HResult=-2147467262
Message=Argument 'DateValue' cannot be converted to type 'Date'.
Source=Microsoft.VisualBasic
When I run:
Dim regDate As Date = Date.Now()
Dim Date1 As String = regDate.ToString("MM.dd.yyyy")
Dim Date2 As String = regDate.ToString("MM-dd")
Dim Date3 As String = regDate.ToString("dd-MM-yyyy")
Dim Date4 As String = regDate.ToString("yyyy/MM/dd")
Dim D1_plus_7 As String = DateAdd("d", 7, Date1)
Dim D2_plus_7 As String = DateAdd("d", 7, Date2)
'Dim D3_plus_7 As String = DateAdd("d", 7, Date3) '< I get the err here
Dim D4_plus_7 As String = DateAdd("d", 7, Date4)
Console.WriteLine(D1_plus_7)
Console.WriteLine(D2_plus_7)
'Console.WriteLine(D3_plus_7)
Console.WriteLine(D4_plus_7)
The commented lines were so I could make sure what would work after I got the error. Oh Date2 was just checking if it would work.
Any help is appreciated.
Thank you
Bobbie