How can I convert the following
string Time = "2324"; string Time = "1024";
In the first case the converted value should be another string "11:24 PM". The second case should be "10:24 AM".
I tried Date.ParseExact(Time,"hh:mm tt",CultureInfo.InvariantCulture) That solves it when the time is less than 12:00PM. But when it is "2324", I get 23:24 PM when I actually need 11:24 PM
Method expects time to be always 4 characters
static string ParseTime(string time)
{
if (int.TryParse(time.Substring(0, 2), out int hrs) && int.TryParse(time.Substring(2), out int min))
{
return new DateTime(2001, 1, 1, hrs, min, 0).ToString("hh:mm tt", CultureInfo.InvariantCulture);
}
// throw exception or return empty;
return string.Empty;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.