1

I am trying to parse a nullable datetime which contains a date and a string which is the time. I would like to be able to put the datetime and the time string together to parse a datetime. But the time and date could be in a inconsistent format. Here is what I have come up with but it breaks for certain formats:

private DateTime? ParseToDateTime(DateTime? dateTime, string time)
{
    if (dateTime.HasValue)
    {
        //Parse the time

        time = TimeToParse(time);
        if (TimeValid(time))
        {
            var inptTime = time.PadLeft(4, '0');
            var hrs = int.Parse(inptTime.Left(2));
            var mins = int.Parse(inptTime.Right(2)) + hrs * 60;
            return DateTime.SpecifyKind(dateTime.Value.Date.Add(TimeSpan.FromMinutes(mins)), DateTimeKind.Local);
        }
    }
    return null;
}

private string TimeToParse(string time)
{
    if (time.Contains("+"))
    {
        time = time.Substring(0, time.IndexOf("+", StringComparison.Ordinal));
    }
    return time;
}

private static bool TimeValid(string time) 
{ 
     if (string.IsNullOrEmpty(time)) return false; 
     if (time.Length < 4) return false; 
     if (time.Length > 4) return false; 
     return true; 
}

I looked into tryparse exact also but the format could be different and we are talking two different values here any idea?

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
TheKeyboarder
  • 57
  • 1
  • 5
  • 2
    You are not giving any examples; how are we supposed to know how to parse it? – Maarten Jan 11 '17 at 09:26
  • forgot private static bool TimeValid(string time) { if (string.IsNullOrEmpty(time)) return false; if (time.Length < 4) return false; if (time.Length > 4) return false; return true; } – TheKeyboarder Jan 11 '17 at 09:27
  • Sorry here are some examples : datetime = 2016-04-23, 23-12-2016, 17/10/2016 time = 23:59, 2359, 23359, 2359+1, 350 – TheKeyboarder Jan 11 '17 at 09:27
  • You can use `DateTime.Parse` to convert your time string to DateTime. I don't understand why are you manually parsing and converting it to minutes... – Pikoh Jan 11 '17 at 09:29
  • Title of your post caught my attention! – RBT Jan 11 '17 at 09:34
  • I have tried DateTime.Parse() but this doesn't parse on 2359 – TheKeyboarder Jan 11 '17 at 09:36
  • @RBT yes, let's not devolve into clickbait titles. – CodeCaster Jan 11 '17 at 09:39
  • Of course...2359 is not a valid time :) But if you are going to get time in that format always, you could try `DateTime dt=DateTime.ParseExact(time,"HHmm",System.Globalization.CultureInfo.InvariantCulture);` – Pikoh Jan 11 '17 at 09:41
  • Your question is currently unanswerable, see duplicate for the idiomatic way: using `DateTime.TryParseExact(string, string[], ...)` where you can pass multiple acceptable formats. If you insist on reinventing the wheel, then elaborate on _"it breaks for certain formats"_ and create a [mcve] including example input and how exactly it "breaks". – CodeCaster Jan 11 '17 at 09:44
  • you are saying - `But the time and date could be in a inconsistent format`. How is that possible? I can understand that `time` parameter is an string so it can be in inconsistent format but why `dateTime` parameter can be in inconsistent format? – RBT Jan 11 '17 at 09:52
  • In your `TimeToParse` function you are making a check for `+` character which can come in input which means you know that `+` can come in your input? Do you know what all possible weird formats are possible which can come in input for `time` parameter? Please add a few sample inputs. – RBT Jan 11 '17 at 09:55
  • Same inputs for time : 12:59+1 1259 0203 tried this TimeSpan.TryParseExact(time.Trim().Replace("+", "").Replace(":", ""), new[] { "HH:mm", "HHmmss" }, CultureInfo.InvariantCulture, TimeSpanStyles.None, out t)) which didn't work for 2359 or 23:59 – TheKeyboarder Jan 11 '17 at 10:55

0 Answers0