How do I convert a 12 hour time string into a C# TimeSpan?
Asked Answered
S

5

15

When a user fills out a form, they use a dropdown to denote what time they would like to schedule the test for. This drop down contains of all times of the day in 15 minute increments in the 12 hour AM/PM form. So for example, if the user selects 4:15 pm, the server sends the string "4:15 PM" to the webserver with the form submittion.

I need to some how convert this string into a Timespan, so I can store it in my database's time field (with linq to sql).

Anyone know of a good way to convert an AM/PM time string into a timespan?

Strop answered 11/1, 2011 at 18:31 Comment(0)
C
39

You probably want to use a DateTime instead of TimeSpan. You can use DateTime.ParseExact to parse the string into a DateTime object.

string s = "4:15 PM";
DateTime t = DateTime.ParseExact(s, "h:mm tt", CultureInfo.InvariantCulture); 
//if you really need a TimeSpan this will get the time elapsed since midnight:
TimeSpan ts = t.TimeOfDay;
Camelopardus answered 11/1, 2011 at 18:35 Comment(4)
This example will not actually work for string such as "11:00 PM". The uppercase "H" format indicates a 24-hour clock. Using a lowercase "h" fixes that problem.Magellan
That's a really important detail. Confirmed and edited.Soluble
This works if the time is in "h:mm tt" format i.e. 4.15 PM, but does not work when time is in "hh:mm tt" format i.e. 12.45 PM. Is there a format that works for both?Appoint
@Appoint you can use the string[] overload for formats to support multiple formats from a single parseDustin
O
7

Easiest way is like this:

var time = "4:15 PM".ToTimeSpan();

.

This takes Phil's code and puts it in a helper method. It's trivial but it makes it a one line call:

public static class TimeSpanHelper
{        
    public static TimeSpan ToTimeSpan(this string timeString)
    {
        var dt = DateTime.ParseExact(timeString, "h:mm tt", System.Globalization.CultureInfo.InvariantCulture);            
        return dt.TimeOfDay;
    }
}
Ontina answered 7/7, 2012 at 17:12 Comment(2)
The format string should be "h:mm tt" or if no space between "AM/PM" then "h:mmtt".Pilate
@DanRandolph thanks for the suggestion, I’ve edited the solution as mentioned.Ontina
D
3

Try this:

DateTime time;
if(DateTime.TryParse("4:15PM", out time)) {
     // time.TimeOfDay will get the time
} else {
     // invalid time
}
Dunlavy answered 11/1, 2011 at 18:41 Comment(0)
C
2

I like Lee's answer the best, but acermate would be correct if you want to use tryparse. To combine that and get timespan do:

    public TimeSpan GetTimeFromString(string timeString)
    {
        DateTime dateWithTime = DateTime.MinValue;
        DateTime.TryParse(timeString, out dateWithTime);
        return dateWithTime.TimeOfDay;
    }
Clinical answered 8/11, 2013 at 22:36 Comment(0)
S
1

Try:

string fromServer = <GETFROMSERVER>();
var time = DateTime.Parse(fromServer);

That gets you the time, if you create the end time as well you can get Timespans by doing arithmetic w/ DateTime objects.

Salient answered 11/1, 2011 at 18:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.