Home > other >  Convert time duration from String to TimeSpan
Convert time duration from String to TimeSpan

Time:05-27

I have a string that contains day of the week, time and duration. E.g. Monday,10:00 AM,45m

The duration could be in either of the following formats:

  1. 45m
  2. 1h45m
  3. 1h

Now I need to convert this into a date with time for both the start of the event and end of the event based on the duration.

I managed to convert this piece "Monday,10:00 AM" into the upcoming date and time for whatever the day of the week is so now I have a datetime as let's say "05/30/2022 10:00:00 AM".

Now I need to create a datetime object for the end time of the event by adding e.g. "45m" to the previous datetime. I don't know the format of the duration piece but it will be one of three from the list above.

How do I convert this into a standard timespan to add to the previous time? Is the above format a standard format that perhaps has a built in way to parse? It's coming from an API.

I have tried this and it works but I'm not sure how to detect and handle the formats.

\\split the original string so now I have duration


\\when I have just the hour duraton e.g. 1h
t = TimeSpan.ParseExact(durationString, "h\\h", CultureInfo.InvariantCulture);
var finalDate = dt.Add(t);

\\when I have just the minute format e.g. 45m

t = TimeSpan.ParseExact(durationString, "m\\m", CultureInfo.InvariantCulture);

CodePudding user response:

Use a ParseExact method overload that accepts an array of formats.

var values = new string[] { "45m", "1h45m", "1h" };
var formats = new string[] { @"m\m", @"h\hm\m", @"h\h" };

foreach (var value in values)
{
    var ts = TimeSpan.ParseExact(value, formats, CultureInfo.InvariantCulture);
    Console.WriteLine(ts);
}

CodePudding user response:

You can use REGEX to check for matching patterns:

using System.Text.RegularExpressions;

            Regex HourOnly = new Regex("^[0-9] h$");
            Regex MinuteOnly = new Regex("^[[0-9] m");
            Regex HourAndMinute = new Regex("^[0-9] h[0-9] m$");

            List<string> conditions = new List<string>();
            string Condition1 = "Monday,10:00 AM,45m";
            string Condition2 = "Monday,10:00 AM,1h45m";
            string Condition3 = "Monday,10:00 AM,1h";
            conditions.Add(Condition1);
            conditions.Add(Condition2);
            conditions.Add(Condition3);

            foreach(string condition in conditions)
            {
                if (HourOnly.IsMatch(condition.Split(',').Last()))
                {
                    Console.WriteLine($"Hour only: {condition}");

                }
                else if (HourAndMinute.IsMatch(condition.Split(',').Last()))
                {
                    Console.WriteLine($"Hour and minute: {condition}");

                }
                else if (MinuteOnly.IsMatch(condition.Split(',').Last()))
                {
                    Console.WriteLine($"Minute only: {condition}");

                }
            }

Granted users can enter 99999h99999m but if you are reasonably sure that won't happen the above regex should suit you just fine.

Here is some additional documentation to aid you on your quest: https://docs.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference

  •  Tags:  
  • c#
  • Related