7

I currently am reading from a text file an assortment of data, and parsing out everything. One of the items being parsed out is a start time for an event in the format of:

yyMMddHHmm
1306050232 

I then parse out to the following:

string year = "20" + time[0].ToString() + time[1].ToString();
string month = time[2].ToString() + time[3].ToString();
string day = time[4].ToString() + time[5].ToString();
string hour = time[6].ToString() + time[7].ToString();
string minute = time[8].ToString() + time[9].ToString();
string ampm ="";
int hourInt = Convert.ToInt32(hour);

if (hourInt <= 12)
{
    time = month + "." + day + "." + year + "@" + hour + ":" + minute + " " + "AM";
    ampm= "AM";                         
}
else
{
    hourInt = hourInt - 12;      
    time = month + "." + day + "." + year + "@" + hourInt.ToString() + ":" + minute + " " + "PM";
    ampm= "PM";  
}

once these are parsed out, i combined the variables, and try to put it into a DateTime.

string tempStartTime = year + "-" + month + "-" + day + " " + hour + ":" + minute + " " + ampm;

string starttime = DateTime.ParseExact(tempStartTime, "yyyy-MM-dd HH:mm tt",null);

my issue is, I get a warning like this from the try catch:

System.FormatException: String was not recognized as a valid DateTime.
   at System.DateTime.ParseExact(String s, String format, IFormatProvider provider)
   at Project.GUI.parseSchedule(Int32 count) 

I don't understand why, or how to correctly do this.

All I want is to take the start time from the file, convert it to a datetime object, and operate on it afterwards.

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
PhoenixLament
  • 741
  • 1
  • 11
  • 32
  • This question here might help you: http://stackoverflow.com/questions/1951004/convert-dd-mm-yyyy-hhmmss-fff-from-string-to-datetime-in-c-sharp?rq=1 –  Jun 05 '13 at 01:33

4 Answers4

15

Why not simply parse with the format you are starting with?

var dt = DateTime.ParseExact(time, "yyMMddHHmm", CultureInfo.InvariantCulture);

There's no need for all of the pre-processing you're doing.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
4

Parsing before parsing is generally quite unnecessary. If you have the input string

//                        yyMMddHHmm
string timestampString = "1306050232";

Then you should be able to do:

CultureInfo provider = CultureInfo.InvariantCulture;
DateTime timestamp = DateTime.ParseExact(timeStampString, "yyMMddHHmm", provider);

If not, I would like to have more information about the exact error you are getting.

Jan Dörrenhaus
  • 6,581
  • 2
  • 34
  • 45
2

Have a look at DateTime.ParseExact() http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx

  DateTime result=null;
  CultureInfo provider = CultureInfo.InvariantCulture;
  // Parse date and time with custom specifier.
  string dateString = "Sun 15 Jun 2008 8:30 AM -06:00";
  string format = "ddd dd MMM yyyy h:mm tt zzz";
  try {
     result = DateTime.ParseExact(dateString, format, provider);
     Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());
  }
  catch (FormatException) {
     Console.WriteLine("{0} is not in the correct format.", dateString);
  }
mason
  • 31,774
  • 10
  • 77
  • 121
1

You might want to look into custom formatters rather than trying to parse everything. I think that would make your code more maintainable, and probably somewhat easier to decode. There's a tool linked on that page that would let you test your format string before putting it into code, as well.

Joel
  • 5,618
  • 1
  • 20
  • 19