I've been doing this, is it possible to get the middle string in datetime.
Basically my string return this '12/23/2015 12:00:00 AM'.
However, what i want is actually '15' from the year 2015.
I've been using something like this 'strDate = strDate.Substring(0, 9)',
which sometimes return me wrong when the date is something like this '1/2/2015 12:00:00 AM'
Can someone help me on this, i am not sure if we can get the middle.
Best How To :
Don't use string methods for this, use
Dim str = "12/23/2015 12:00:00 AM"
Dim dt As DateTime
If DateTime.TryParseExact(str, "MM'/'dd'/'yyyy hh:mm:ss tt", Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, dt) Then
Dim yearOnly As String = dt.ToString("yy") ' 15
CultureInfo.InvariantCulture and this format you don't even need to use
TryParseExact, you can use
DateTime.TryParse(str, CultureInfo.InvariantCulture, DateTimeStyles.None, dt)
MSDN: Custom Date and Time Format Strings, The "yy" Custom Format Specifier