Convert String to Date Time
28,404
Solution 1
You can use DateTime.ParseExact
.
DateTime date = DateTime.ParseExact(x, "yyyyMMddHHmmss", CultureInfo.CurrentCulture);
VB
Dim myDate as DateTime = DateTime.ParseExact(x, "yyyyMMddHHmmss", CultureInfo.CurrentCulture)
Solution 2
Use the DateTime.ParseExact
function in conjunction with the exact format of your input string. Example:
C#:
string input = "20110617111051";
string format = "yyyyMMddhhmmss";
DateTime dateTime = DateTime.ParseExact(input, format, CultureInfo.InvariantCulture);
VB:
Dim input As String = "20110617111051"
Dim format As String = "yyyyMMddhhmmss"
Dim dateTime as DateTime = DateTime.ParseExact(input, format, CultureInfo.CurrentCulture)
See this page for more info on custom date and time strings.
Author by
MNVR
Updated on June 18, 2020Comments
-
MNVR over 3 years
Possible Duplicate:
Convert string to DateTime in c#I am trying to convert a string in the format "20110617111051" to date time. Currently I am using String.SubString() function to extract year, month, day, time to format a standard string and then using Convert.ToDateTime(string). Is there any other simple way to do this?
Dim x as String="20110617110715" Dim standard as string = x.SubString(0,4) & "-" & x.SubString(4,2) & "-" & x.SubString(6,2) 'and time Dim dateTime as DateTime = Convert.ToDateTime(standard)