Re: Safe conversion from string to DateTime

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



I have tried DateTime.Parse but it threw an exception on "12 noon".

Hans


On 5 May 2005 07:15:22 -0700, SCDeveloper wrote:

> use DateTime.Parse or DateTime.ParseExact
>
> Parse ignores leading and trailing white space, and unrecognized
> characters if possible, and it fills in missing information with the
> corresponding current date and time values. Parse will throw a
> FormatException, though, if it's unable to decipher the string you send
> to it.
>
> DateTime.Parse will parse a valid date and time from a string. The
> string must contain the representation of a date and time in one of the
> formats described in the DateTimeFormatInfo object.
>
> For DateTime.ParseExact, the string that you pass to it must exactly
> match the format that you specify in the IFormatProvider parameter.
>
> Example for DateTime.Parse
>
> NewDate = DateTime.Parse("10/27/61 08:47");
> NewDate = DateTime.Parse("10/1961");
> NewDate = DateTime.Parse("27 October 1961 8:47 pm");
>
> IFormatProvider format = new
> System.Globalization.CultureInfo("fr-FR", true);
> string[] expectedFormats = {"g", "G", "f", "F"};
>
> // This is DD/MM/YYYY format
> NewDate = DateTime.ParseExact("27/10/1961 08:47:00",
> expectedFormats, format,
> System.Globalization.DateTimeStyles.AllowWhiteSpaces);
> Console.WriteLine("Parsed DD/MM/YY: {0}", NewDate.ToString());
>
>
> Example for DateTime.ParseExact
>
> try
> {
> NewDate = DateTime.ParseExact(
> "10/27/1961",
> expectedFormats,
> format,
> System.Globalization.DateTimeStyles.AllowWhiteSpaces);
> Console.WriteLine(NewDate.ToString());
> }
> catch (Exception e)
> {
> Console.WriteLine(e.Message);
> }
>
> Hope this helps.
>
> SCDeveloper
> http://www.sharingcorner.com
.



Relevant Pages

  • US vs UK datetime/string format failing on certain machines. Ideas
    ... I have a webform with a text box and a calendar control. ... or 6 lines of code just to format something from a text box ... String was not recognized as a valid DateTime. ... An unhandled exception occurred during the execution of the ...
    (microsoft.public.dotnet.framework.aspnet)
  • Why does this cause an error? (2005)
    ... The datatype in Sql Server is varchar, ... I'm using a procedure to format the number as follows: ... This flies but produces a "first chance exception" that "Input string ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Safe conversion from string to DateTime
    ... Parse ignores leading and trailing white space, ... DateTime.Parse will parse a valid date and time from a string. ... match the format that you specify in the IFormatProvider parameter. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: importdata problem
    ... if is fixed width you can set up a format string to parse>> it; if not you'll have to do something like read each line into a>> string variable a la fgetland parse each line deciding on ... >> What would a format string look like? ...
    (comp.soft-sys.matlab)
  • Re: Put tabs into Strings
    ... but I get the separating String from a gui as ... >How shall I parse (or format) the input to get a real tab in the ...
    (comp.lang.java.programmer)