If date is (certain date) then - vb.net

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!

Related

subtract from one date another

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_

how to store all days beween two days using vb.net

i need to store all the days between two dates in an ARRAY.
for eg:
from_date is: 2014-09-20, to_date : 2014-09-24
the result will be
2014-09-20, 2014-09-21, 2014-09-22, 2014-09-23, 2014-09-24.
Any help will be appreciated..
One way: LINQ
Dim days = From day In Enumerable.Range(0, (toDate - fromDate).Days + 1)
Select fromDate.AddDays(day)
Dim result As Date() = days.ToArray()
The classic way without math which also works in .NET 2:
Dim dayList As New List(Of Date)
Dim currentDate = fromDate
While currentDate <= toDate
dayList.Add(currentDate)
currentDate = currentDate.AddDays(1)
End While
Dim result As Date() = dayList.ToArray()
If you need a string() as commented just append ToString(). For example:
Dim days = From day In Enumerable.Range(0, (toDate - fromDate).Days + 1)
Select fromDate.AddDays(day).ToString()
Dim result As String() = days.ToArray()

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.

How can i retrieve date time available per second in one day?

my input is date.
But, i'm stuck on how to retrieve date time in every second.
I need to put the each second date time in the 2d array.so my array(0,0) should equal to 2/10/2014 00:00:00 AM and array(86399,0) is equal to 2/10/2014 23:59:59 PM.
i tried do looping as per below code:
Dim twoDarray(86399, 1) As String
Dim dtInput As Date
dtInput= #2/10/2014#
For i=0 to 86399
twoDarray(i, 0) = dtInput
dtInput = dtInput +second 'i know this not right
Next
I just don't know how to increase date time every second in right way.
Please help.
Have you thought about something along the lines of
Using a Datetime (MSDN Datetime)
dtInput= new DateTime(2014,10,2)
For i=0 to 86399
twoDarray(i, 0) = dtInput
dtInput = dtInput.AddSeconds(1)
Next
Or
dtInput= new DateTime(2014,10,2)
For i=0 to 86399
twoDarray(i, 0) = dtInput.AddSeconds(i+1)
Next
You can try following method also
Dim dtFrom As New DateTime(2014, 10, 2, 0, 0, 0)
Dim dtTo As New DateTime(2014, 10, 2, 23, 59, 59)
Dim iFirstDim As Integer = (dtTo - dtFrom).TotalSeconds
Dim iSecondDim As Integer = 10
Dim arrTime(iFirstDim, iSecondDim) As String
Dim i As Integer = 0
Do While (dtTo > dtFrom)
arrTime(i, 0) = dtFrom.ToString("d/MM/yyyy HH:mm:ss")
dtFrom = dtFrom.AddSeconds(1)
i += 1
Loop
HOW TO USE IT?
Dim dtResult As DateTime
If DateTime.TryParseExact(arrTime(150, 0), "d/MM/yyyy HH:mm:ss", Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, dtResult) Then
MsgBox(dtResult.ToString("yyyy-MM-dd HH:mm:ss"))
End If

How use loops by datetime each on weekly in vb.net

i try loop while with datetime each on weekly in VB.NET 2008.
This Code
Private Sub Button1_Click()....
'Select DateTime
Dim strDate As Date = dateTimePicker.Value.ToString("yyyy-MM-dd")
'one week (+7)
Dim strDateWeek As String = DateAdd("d", +7, dateTimePicker.Value.ToString("yyyy-MM-dd"))
'DateCurrent
Dim strDateNow As String = DateAdd("d", 0, Now.ToLongDateString())
'While strDate < strDateNow
'ListBox1.Items.Add(strDateWeek)
'End While
ListBox1.Items.Add(strDateWeek)
End Sub
Example
I select on datetimepicker at "04/02/2013"
Output now: 11/02/2013
But I need Output each on weekly
11/02/2013
18/02/2013
25/02/2013 >>> To Current Week
I try loop While, But don't work.
Thanks you for your time. :)
You could do a while loop until the datetime is greater than today?
You want to use DateTime rather than Date, so you can compare to a DateTime.Now
You want to set your actual DatePicker value to a variable, else it will always be the same and you will just get an infinite loop.
Dim datePickerValue As DateTime = DateTimePicker.Value
Dim strDate As Date = DateTimePicker.Value.ToString("yyyy-MM-dd")
Dim strDateWeek As String
Dim strDateNow As String = DateAdd("d", 0, Now.ToLongDateString())
While datePickerValue < DateTime.Now()
strDateWeek = DateAdd("d", +7, datePickerValue.ToString("yyyy-MM-dd"))
datePickerValue = DateAdd("d", +7, datePickerValue.ToString("yyyy-MM-dd"))
ListBox1.Items.Add(strDateWeek)
End While
Just done it on my VS using your naming conventions and this works fine for me
It's been a long time since I didn't have used VB, but maybe I can help?
In your code, using while could be a wrong choice perhaps you could use a for with a break instead.
for I = 1 to 10
Dim strDateWeek As String = DateAdd("d", +7 * i, dateTimePicker.Value.ToString("yyyy-MM-dd"))
.
.
.
or
while(...)
I += 1
Dim strDateWeek As String = DateAdd("d", +7 * i, dateTimePicker.Value.ToString("yyyy-MM-dd"))
Hope that helps.
Try this:
Dim dtAux As Date = dateTimePicker.Value
Dim dtEnd As Date = Date.Today.AddDays(7 - dt.DayOfWeek)
While dtAux <= dtEnd
ListBox1.Items.Add(dtAux.ToString("yyyy-MM-dd"))
dtAux = dtAux.AddDays(7)
End While
The date dtEnd is the last day of the current week, if you want the loop to stop on the current date simply change the while condition to:
While dtAux <= Date.Today