using System.Globalization;
using System.Text.RegularExpressions;
string[] inputs = { "6 months", "3months", "1 year", "2 years", "1 month" };
foreach (string input in inputs)
TimeSpan timeSpan = ParseToTimeSpan(input);
Console.WriteLine($"Input: {input}, TimeSpan: {timeSpan}");
public static TimeSpan ParseToTimeSpan(string input)
Regex regex = new Regex(@"(\d+)\s*([a-zA-Z]+)");
Match match = regex.Match(input);
throw new ArgumentException("Invalid input format.");
int value = int.Parse(match.Groups[1].Value);
string unit = match.Groups[2].Value.ToLower(CultureInfo.InvariantCulture);
return TimeSpan.FromDays(value * 30.44);
return TimeSpan.FromDays(value * 365.25);
throw new ArgumentException("Invalid time unit.");