how to convert the string to date in vb.net - vb.net

I AM FACING ONE PROBLEM.
I AM USING BELOW METHOD FOR MAKING STRING VALUE TO DATE.
string values is like this ="01/12/2002" like "dd/MM/YYYY"
My problem is that.
two string values
->1)01/01/2025
->2)1/1/2025
i am getting the Value like above 1 or 2
Dim d As Date = DateTime.ParseExact(dumm, "dd/MM/yyyy", Nothing)
if 1 comes nothing will happen but
if i get 2 i am facing error like String was not recognized as a valid DateTime.
As per my Analysts what i understood is date should be 2 digits Remaining all are two digits other wise giving the error.
but some times i am getting single digits from the excel to vb.net
how can i solve this issue...
Dim dumm As String = DtSet3.Tables(0).Rows(k + 0).Item(3).ToString
Dim d As Date = DateTime.ParseExact(dumm, "d/M/yyyy", Nothing)
i put the break point on dumm ok dumm vlaues= "1/2/2026 12:00:00 Am"
Error...............
now
Dim dumm As String = DtSet3.Tables(0).Rows(k + 0).Item(3).ToString
Dim d As Date = DateTime.ParseExact("01/02/2026", "dd/MM/yyyy", Nothing)
Working Fine.......

Use the date format string "d/M/yyyy h:m:s tt". This will handle both cases i.e. with and without leading zero for the day and month. Additionally, since your actual variable has a time component in addition to the date, you need to add the format string for parsing time as well.
However, I would advise you to use TryParseExact, which will return boolean values based on success or failure of the parse rather than throwing exceptions.
Demo for using TryParseExact along with appropriate format string.

Related

convert Integers to Date Objects VB

I have 2 integers and 1 String. Now I want to convert it in date objects and display it in datepicker1 (I set its format to "short")
for example:
Dim Day As Integer = 14
Dim Month As String = "July"
Dim Year As Integer = 2016
I am expecting an answer as
7/14/2016 'this must be displayed in datepicker1
I searched through the internet and i found the same question here but this is from different language and i want it in VB. if my question has already been asked before. please post the link.
I am new to vb so i am hoping for your help guys!
Here's a way that uses Date.ParseExact:
Dim dt = Date.ParseExact($"{Year}-{month}-{day}", "yyyy-MMMM-dd", DateTimeFormatInfo.InvariantInfo)
Note that i'm using string interpolation at $"..." which is available in VS 2015/VB14. Otherwise you could use String.Format:
Dim dtStr = String.Format("{0}-{1}-{2}", Year, month, day)
Dim dt = Date.ParseExact(dtStr, "yyyy-MMMM-dd", DateTimeFormatInfo.InvariantInfo)
Here you'll find more informations about the custom date and time format strings.

String was not recognize as a Valid datetime in vb.net

I'm trying to highlight all the rows in the datagridview which date is more than five days from the date today.
Here is my code.
For Each row As DataGridViewRow In dgv_Transfer_ledger.Rows
Dim now As DateTime = Date.Now
Dim delayDate = Date.Parse(row.Cells(0).Value.ToString)
Dim fiveDaysbefore = delayDate.AddDays(-5)
If now > fiveDaysbefore AndAlso now < delayDate Then
row.DefaultCellStyle.BackColor = Color.Yellow
ElseIf now > delayDate Then
row.DefaultCellStyle.BackColor = Color.Red
End If
Next
I'm getting the error String was not recognized as a valid DateTime.
Why I'm getting this error? Can anyone help me please? Thanks in Advance!
is your date in cell showing the correct format?
as they said, you need to check your cell's data and make sure it is in the correct format before they can actually run your code
if you know the format you may try
EG. (DD/MM/YY) (MM/DD/YY) [or something like this]
Dim delayDate = Date.ParseExact
I already got the answer.
Dim currentdate As Date = Date.Now.ToString("d")
You have to format the date based on the datatype in your datagridview.
I didn't use parse or ParseExact for getting the delaydate. Just make sure that the cell format in datagridview is the same format of the current date :)
Thanks for the reference guys :)

Parse a text string into fields based on text and position VB

I have a .text file of strings delimited by spaces. That would be the obvious parsing solution when I bring it into Excel or Access, but since the strings are not the same size, the fields will be incorrect. I'm hoping to find some help here as I'm not really experienced with this.
This is an example section of the string:
259998887575 15 00:14:38 C33 0:14:42 T33 00:14:52 00:14:58
202224446898 33 00:16:24 B23 00:17:00 C31 00:17:15 T31 00:19:30 C04 00:17:15 T28 00:19:30 00:19:32
The numbers with colons are time stamps and the letter codes (T/C/B) all represent a different field type. My problem is that there can be any number of C and T time stamps listed in the string and there may or may not be a B time stamp.
I'd like the result to show 4 fields... the first, the first c time stamp, the last t time stamp and the last time stamp (the bolded time stamps). All other information can be disregarded. I'd like to use VB to cycle through due to the number of records.
Any help is appreciated!
Edit: It seems like you want to split the dates into types, determined by the bit before each letter.
You can use the Strings.Split method to divide the lines at the spaces. Then use the Date.TryParse method to check which parts are Dates (or times, it's the same). Then you select the first letter of the previous part and sort the dates into their respective lists.
Dim s As String = "202224446898 33 00:16:24 B23 00:17:00 C31 00:17:15 T31 00:19:30 C04 00:17:15 T28 00:19:30 00:19:32"
Dim parts() As String = Strings.Split(s, " ") 'Split the string at all spaces
'Create lists of Date, one list for all dates, one list for each Letter
Dim Tdates As New List(Of Date) 'This stores converted dates
Dim Cdates As New List(Of Date) 'This stores converted dates
Dim Bdates As New List(Of Date) 'This stores converted dates
Dim Alldates As New List(Of Date) 'This stores converted dates
For i = 0 To parts.Count - 1 'Iterate through all parts
Dim ThisDate As Date
If Date.TryParse(parts(i), ThisDate) = True Then 'This tries to parse the current part as a date, returns True if the part is a date
Alldates.Add(ThisDate) 'You want to store all dates in this list
If i > 0 AndAlso parts(i - 1).Length > 0 Then
Dim PrevPartsFirstLetter As String = parts(i - 1)(0) 'Crop the first letter of the previous part
Select Case PrevPartsFirstLetter
Case "T" : Tdates.Add(ThisDate)
Case "C" : Cdates.Add(ThisDate)
Case "B" : Bdates.Add(ThisDate)
End Select
End If
End If
Next
'You can now select the parts you want from the lists
The Tdates-List contains all dates which have a T thing before them and so on. The AllDates-List contains all dates in the line.

vb.net rowfilter a datetime of 15 minutes ago

I hope someone can help, I've been struggling with this for a few days now.
I am trying to filter out some records using a rowfilter that has a datetime in one of the columns that is older than 15 minutes ago, looking at it, this looks to me as though it would do it:
Dim TimeStr As String
TimeStr = String.Format(CultureInfo.InvariantCulture, "#{0}#", Now.AddMinutes(-15))
AlertsView.RowFilter = "StatusId < 3 AND LastEmailed < " & TimeStr
Before the filter there are 6 rows in the view but after the filter there are none, i know the StatusId field is correct as if i filter on just that it works fine. All 6 rows have a LastEmailed value of 2013-09-03 23:06:44.813
I'm thinking it might be something with the string conversion but i am stuck.
Many thanks
Anything between the two # characters in a RowFilter expression will be ultimately passed into DateTime.Parse(s, CultureInfo.InvariantCulture).
This should be mentioned in the MSDN documentation for DataColumn.Expression, but it isn't. However, if you dig really deep you can find it in the constructor of the internal class System.Data.ConstNode.
So as long as you are generating values that are unambiguous in the invariant culture, you should be fine. Just to be on the safe side, you might want to use the "s" format specifier to give you a value like 2013-12-31T12:34:56. Change your format string to "#{0:s}#".
Or much cleaner:
Dim TimeStr As String
TimeStr = Now.AddMinutes(-15).ToString("s", CultureInfo.InvariantCulture)
AlertsView.RowFilter = "StatusId < 3 AND LastEmailed < #" & TimeStr & "#"
String.Format doesn't buy you much here.
Also - just out of curiosity, are you sure you are not comparing apples to oranges? It wouldn't be uncommon for a database to store UTC time in a field like LastEmailed. You might need to use DateTime.UtcNow instead of just Now (which is an alias for DateTime.Now).

convert numeric up down value to time format

I have one NumericUpDown1 control
if i select 5 in numeric up down control.the i am calling that value like this:
NumericUpDown1.Value
that return value 5 like integer instead of getting 5.
i want to get that value in time format
I if i select 5.that should return 00:05:00..
how i can convert numeric up down value to times..
in my data base i want to store that value the data type time(7).I tryed something like this:
Dim value As TimeSpan = Convert.ToDecimal(NumericUpDown1.Value)
but that is getting error
You are using TimeSpan wrongly, better rely on Date:
Dim value As Date = New Date(Now.Year, Now.Month, Now.Day, 0, NumericUpDown1.Value, 0)
You can convert this variable into a string with the format you want by doing:
Dim valueAsString As String = value.ToString("HH:mm:ss")
This is the right way to use TimeSpan:
Dim value As TimeSpan = New TimeSpan(0, NumericUpDown1.Value, 0)
But I do recommend the Date alternative above. Use TimeSpan just for measuring intervals (between Date type variables) and better store date/time-related information as Date, which is easier to deal with.