Question How to convert the given string into the formatted date and time

Just to clarify, you want 110218055515 to become
11/02/18 05:55:15
?

VB.net doesn't store dates as strings, so you'd need to artificially process the string.
One option: You could treat it as an array or use the strings.mid function to get individual 2 digit fragments.
You can then use the Val function to turn each 2 digit fragment into an integer. Have one for days, one for months, etc.
Finally use these integers to build up a datetime value.

An alternative is to process the string to add in the "/", " ", and ":" signs as appropriate before letting VB.net turn it into a datetime value for you.
VB.NET:
[FONT=Microsoft Sans Serif]        Dim s As String = "110218055515"               ' start with your number format date.
        s = Mid(s, 1, 2) & "/" & Mid(s, 3, 2) & "/" & Mid(s, 5, 2) & " " & Mid(s, 7, 2) & ":" & Mid(s, 9, 2) & ":" & Mid(s, 11, 2) 'Replace number format date with string format date.
        Dim t As DateTime     'Defines t as a datetime value
        t = DateTime.Parse(s)[/FONT][FONT=Microsoft Sans Serif] ' Turns t into the datetime value of the correctly formatted s
msgbox(t) ' Shows you what you have.
[/FONT]
 
string s = "110218055515" converted into the datetime format as dd/mm/yy hh:mm:ss
First convert it to a Date value, then convert that to a display string. You don't have to set delimiter for parsing the to Date, you can specify the parsing format:
VB.NET:
Dim d As Date = Date.ParseExact(s, "yyMMddHHmmss", Nothing)
and if that is the standard display format for your culture then a simple ToString will produce that:
VB.NET:
s = d.ToString
if not you can specify the display format as parameter to the ToString method either specifically; .ToString(String), or by specifying which culture format you wish to output as; .ToString(IFormatProvider). There are code samples for both in the mentioned help topics. DateTime Methods (System)
 
Back
Top