0

I'm trying below code to try to parse time and add it to current date:

string[] sDatetimeFormat1 = { "M/d/yyyy h:mm:ss tt", "M/d/yyyy h:mm tt",
                             "MM/dd/yyyy hh:mm:ss", "M/d/yyyy h:mm:ss",
                             "M/d/yyyy hh:mm tt", "M/d/yyyy hh tt",
                             "M/d/yyyy h:mm", "M/d/yyyy h:mm",
                             "MM/dd/yyyy hh:mm", "M/dd/yyyy hh:mm" };
string[] sDatetimeFormat2 = { "HH:mm tt", "h:m t", "HH:mm:ss",
                             "HH:mm:ss tt", "h:m:s t"};

if (DateTime.TryParseExact(InputDateOrTime.Replace("\"", ""), 
    sDatetimeFormat1, new CultureInfo("en-US"), DateTimeStyles.None, 
    out sStartTime))
{
    //something
}
else if (TimeSpan.TryParse(InputDateOrTime.Replace("\"", ""), out tsInput))
{
    sStartTime = DateTime.Parse(DateTime.Today.ToString() + tsInput);
}

Input might appear as in sDateTimeFormat1 or might as well appear as in sDatetimeFormat2, If it's sDatetimeFormat1 then I need to just parse it. If it's as in sDatetimeFormat2, then I need to add that time to current date.

Ex: "06/03/2016 14:22:00" should be parsed as "06/03/2016 2:22:00 PM"

"2:22 PM" should be parsed as "06/03/2016 2:22:00 PM" (i.e., today + timespan: format is not important, but it should represent the same date-time).

.Net version is 2.0

Uwe Keim
  • 38,279
  • 56
  • 171
  • 280
InfantPro'Aravind'
  • 11,692
  • 23
  • 79
  • 113
  • http://stackoverflow.com/questions/13044603/convert-time-span-value-to-format-hhmm-am-pm-using-c-sharp this one does the reverse of what my requirement is :) – InfantPro'Aravind' Jun 03 '16 at 09:55
  • I don't think you need to do anything. The Net library produces the results you are looking for. – jdweng Jun 03 '16 at 10:25
  • @jdweng, it's not working, it returns false everytime.. – InfantPro'Aravind' Jun 03 '16 at 10:28
  • And starttime will have "01/01/0001 ..." as a result – InfantPro'Aravind' Jun 03 '16 at 10:28
  • The default value of a DateTime is 01/01/0001 when it fails the parse. When it successfully parses there is no issues the date will be set to the current date and you don't have to add anything. So I think what you need to do is when the parse fails either abort the application or set the DateTime to the current date. Not to determine the format of the date. – jdweng Jun 03 '16 at 10:39
  • My motto is to add the timespan to the current date. Not to get the current date time. – InfantPro'Aravind' Jun 03 '16 at 10:48
  • What is the difference between adding 6 hours to 6/3/16 00:00 to get 6/3/16 06:00 or just parssing 6/3/16 0600? – jdweng Jun 03 '16 at 11:13

1 Answers1

0

There are a few problems in the code you provided. The first one is that you try to parse a string representing time as a TimeSpan object. TimeSpan does not represent time, it rather represents a time difference, e.g. 2 hours.

The second problem is that you didn't include the right formatter for the string you showed. It should be "h:mm tt" or simply "t".

The following code does the conversion you wanted:

string[] sDatetimeFormat2 = { "HH:mm tt", "h:m t", "HH:mm:ss",
                         "HH:mm:ss tt", "h:m:s t", "H:mm tt", "h:mm tt"};

DateTime tsInput;
var InputDateOrTime = "2:22 PM";
DateTime.TryParseExact(InputDateOrTime.Replace("\"", ""), sDatetimeFormat2, new CultureInfo("en-US"), DateTimeStyles.None,  out tsInput);
Console.WriteLine(tsInput);

Notice that you don't need to add the current date because DateTime.TryParseExact does it for you. The output for this example is 6/3/2016 2:22:00 PM.

Tomer
  • 1,566
  • 10
  • 18