Creating a DateTime Object from a String

I am currently reading an assortment of data from a text file and parsing everything. One of the shared elements is the time the event started in the format:

yyMMddHHmm 1306050232 

Then I parse 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 they figured out, I combined the variables and try to put them in a DateTime.

 string tempStartTime = year + "-" + month + "-" + day + " " + hour + ":" + minute + " " + ampm; string starttime = DateTime.ParseExact(tempStartTime, "yyyy-MM-dd HH:mm tt",null); 

My problem: I get a warning like this from 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 do it right.

All I want to do is take the start time from the file, convert it to a datetime object, and then continue.

+4
source share
4 answers

Why not just make out the format you're starting from?

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

There is no need for all the preprocessing you do.

+11
source

Parsing before analysis is usually not needed. If you have an input line

 // yyMMddHHmm string timestampString = "1306050232"; 

Then you should be able to:

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

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

+3
source

Take 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); } 
+2
source

You might want to learn custom formats , rather than trying to parse everything. I think this will make your code more maintainable and probably a little easier to decode. There's a tool linked on this page that will let you test your format string before embedding it in your code.

+1
source

Source: https://habr.com/ru/post/1484546/


All Articles