Reputation: 15039
I would like to know if there is a way to convert a 24 Hour time formatted string to a TimeSpan.
Right now I have a "old fashion style":
string stringTime = "07:35";
string[] values = stringTime.Split(':');
TimeSpan ts = new TimeSpan(values[0], values[1], 0);
Upvotes: 74
Views: 193089
Reputation: 241485
For the string "07:35"
given in the question, there are three possible data types to choose from:
TimeSpan
-10675199.02:48:05.4775808
to 10675199.02:48:05.4775807
TimeOnly
00:00:00.000000
to 23:59:59.9999999
Portable.System.DateTimeOnly
.DateTime
0001-01-01 00:00:00.0000000
to 9999-12-31 23:59:59.9999999
0001-01-01
or 2000-01-01
, etc. When doing so, it's important to disregard the date in all usages. Generally, this was done in older software to support parsing am/pm components, but is no longer necessary since .NET 6.All three data types support the following methods:
Parse
- Attempts to parse using a variety of known formats. Throws an exception if parsing does not succeed.
TryParse
- Like Parse
, but returns false
if the parsing does not succeed. Useful for validation logic.
ParseExact
- Attempts to parse using a specific format. Throws an exception if parsing does not succeed.
TryParseExact
- Like ParseExact
, but returns false
if parsing does not succeed. Useful for validation logic.
The ParseExact
and TryParseExact
methods require a format specification, which can be either in a standard form represented by a a single character, or a custom form represented by multiple characters. The format strings differ by data type.
TimeOnly
and DateTime
use the standard or custom date and time format strings.
"07:35"
can be parsed with the custom "HH:mm"
format in all cultures, including the InvariantCulture
."07:35"
can also be parsed with the standard "t"
format in many cultures, including the InvariantCulture
.TimeSpan
uses the standard or custom time span format strings.
"07:35"
can be parsed with the custom @"hh\:mm"
(or "hh\\:mm"
) format in most cultures, including the InvariantCulture
."07:35"
can also be parsed with the standard "c"
or "g"
formats in many cultures, including the InvariantCulture
.I'll demonstrate both TimeSpan
and TimeOnly
. (Generally speaking, DateTime
is no longer needed for such strings.) In all cases, be sure to import both System
and System.Globalization
in your using statements, unless you're already importing them implicitly.
Flexible parsing (using current culture):
TimeSpan duration = TimeSpan.Parse("07:35");
TimeOnly time = TimeOnly.Parse("07:35");
Fixed parsing (performant, using invariant culture):
TimeSpan duration = TimeSpan.ParseExact("07:35", @"hh\:mm",
CultureInfo.InvariantCulture);
TimeOnly time = TimeOnly.ParseExact("07:35", "HH:mm",
CultureInfo.InvariantCulture);
Flexible parsing for validation (using current culture):
TimeSpan duration;
if (!TimeSpan.TryParse("07:35", out duration))
{
// handle validation error
}
TimeOnly time;
if (!TimeOnly.TryParse("07:35", out time))
{
// handle validation error
}
Fixed parsing for validation (performant, using invariant culture):
TimeSpan duration;
if (!TimeSpan.TryParseExact("07:35", @"hh\:mm",
CultureInfo.InvariantCulture, out duration))
{
// handle validation error
}
TimeOnly time;
if (!TimeOnly.TryParseExact("07:35", "HH:mm",
CultureInfo.InvariantCulture, DateTimeStyles.None, out time))
{
// handle validation error
}
Upvotes: 162
Reputation: 15076
Try
var ts = TimeSpan.Parse(stringTime);
With a newer .NET you also have
TimeSpan ts;
if(!TimeSpan.TryParse(stringTime, out ts)){
// throw exception or whatnot
}
// ts now has a valid format
This is the general idiom for parsing strings in .NET with the first version handling erroneous string by throwing FormatException and the latter letting the Boolean TryParse give you the information directly.
Upvotes: 13
Reputation: 4492
You can convert the time using the following code.
TimeSpan _time = TimeSpan.Parse("07:35");
But if you want to get the current time of the day you can use the following code:
TimeSpan _CurrentTime = DateTime.Now.TimeOfDay;
The result will be:
03:54:35.7763461
With a object cantain the Hours, Minutes, Seconds, Ticks and etc.
Upvotes: 3
Reputation: 13652
Use TimeSpan.Parse
to convert the string
http://msdn.microsoft.com/en-us/library/system.timespan.parse(v=vs.110).aspx
Upvotes: 6