How about:
string str = "1000";//drv.Row("arrival_time")
string[] formats = new string[] { "HHmm" };
DateTime dt = DateTime.ParseExact(str, formats,
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.AdjustToUniversal);
string strTime = dt.ToShortTimeString();
DateTime dte = DateTime.Now;
IFormatProvider culture = new System.Globalization.CultureInfo("en-GB", true);
VB.NET
Dim str As String = "1000" 'drv.Row("arrival_time")
Dim formats As String() = New String() {"HHmm"}
Dim dt As DateTime = DateTime.ParseExact(str, formats, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AdjustToUniversal)
Dim strTime As String = dt.ToShortTimeString()
Dim dte As DateTime = DateTime.Now
Dim culture As IFormatProvider = New System.Globalization.CultureInfo("en-GB", True)
Outputs:
10:00 AM
Alternatively, if you plan on using that frequently, you might want to add that as an extension method.
public static class Extensions
{
public static string ToTime(this string str)
{
DateTime dt = DateTime.Now;
try
{
string[] formats = new string[] { "HHmm" };
dt = DateTime.ParseExact(str, formats,
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.AdjustToUniversal);
}
catch
{ throw new Exception("Invalid data"); }
return dt.ToShortTimeString();
}
}
--VB.NET--
Public NotInheritable Class Extensions
Private Sub New()
End Sub
<System.Runtime.CompilerServices.Extension> _
Public Shared Function ToTime(str As String) As String
Dim dt As DateTime = DateTime.Now
Try
Dim formats As String() = New String() {"HHmm"}
dt = DateTime.ParseExact(str, formats, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AdjustToUniversal)
Catch
Throw New Exception("Invalid data")
End Try
Return dt.ToShortTimeString()
End Function
End Class
Example:
string str = "13000";//drv.Row("arrival_time")
string strTime = str.ToTime();
VB.NET
Dim str As String = "13000"
'drv.Row("arrival_time")
Dim strTime As String = str.ToTime()
Anything bad in the data will throw you an exception.
--EDIT--
Works fine in VB.NET.