Question

DateTime datuMDokumenta = Convert.ToDateTime(txtDatumDokum.Text);

txtDatumDokum.Text is like "09.09.2011".

but i get FormatException error. Must i parse date?

Was it helpful?

Solution

Try DateTime.ParseExact with the dd.MM.yyyy format string

 DateTime.ParseExact(txtDatumDokum.Text, "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None);

OTHER TIPS

It's not good to see, anyway try this:

string s = "09.09.2011";
DateTime dt = Convert.ToDateTime(
    s.Replace(".",
    new System.Globalization.DateTimeFormatInfo().DateSeparator));

You need to tell us why the text input is using this format. If it is because the user enters it this way, then you need to make sure that the format matches that given by Thread.CurrentCulture.DateTimeFormat.ShortDatePattern. Changing the culture (by setting Thread.CurrentCulture) to an appropriate value will then solve your problem.

If you are supposed to parse the input no matter what format it is in, then you will need to do some manual processing first (perhaps remove spaces and other delimiter characters from the input with string.Replace) and then try to parse the date using DateTime.ParseExact and a known format string.

But it all depends on why the input has that format, and why your application's current culture does not match it.

You could try this, TryParse avoids parsing exceptions.. Then you just need check result to be sure that it parsed.

DateTime datuMDokumenta;
bool result = DateTime.TryParse(txtDatumDokum.Text, out datuMDokumenta);

You will have to determine if this is a good solution for your application.

See this example: http://msdn.microsoft.com/en-us/library/ch92fbc1.aspx

Judging by the date you gave you need to include a culture, de-DE accepts 01.01.11 type of dates but I'm not sure which one you actually want to use, you'll need to decide that.. the Code would look like this:

using System.Globalization;

DateTime datuMDokumenta;
bool result = DateTime.TryParse(txtDatumDokum.Text, CultureInfo.CreateSpecificCulture("de-DE"), DateTimeStyles.None, out datuMDokumenta);

A list of cultures can be found here, select the appropriate one for you: http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo%28v=vs.71%29.aspx

The plus here is that this code is a bit more work but it is very difficult to break. Assuming you are using a free text entry on a TextBox you don't want to be throwing exceptions.

Yes you have to parse input date in current culture.

string[] format = new string[] { "dd.MM.yyyy" };
string value = "09.09.2011";
DateTime datetime;

if (DateTime.TryParseExact(value, format, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.NoCurrentDateDefault, out datetime))
      //Valid
else
     //Invalid

DateTime dt = Convert.ToDateTime(txtDatumDokum.Text)

It is right...there is no isssue

During a Deserialization call under compact framework 3.5 i've had some unexpected behaviour before.

I've converted from using the OpenNETCF serialization classes to the framework XML serialization class. In doing so, the default time format has changed and the order of property/public members. So long story short, i've exposed a text property which converts my date-times back to the format my VB6 application is expecting.

            Dim dumbDate As New Date
            Dim formats() As String = {"yyyy-MM-ddTHH:mm:ss.fffzzz", _
                                       "yyyy-MM-dd HH:mm:ss:fffffffzzz"}

            _datetimeTaken = dumbDate.ParseExact(value, formats, CultureInfo.InvariantCulture, DateTimeStyles.None)

            ' There is something wrong with compact framework during the Serialization calls. 
            ' calling the shared method Date.Parse or Date.ParseExact does not produce the same
            ' result as calling a share method on an instance of Date. WTF?!?!?!
            ' The below will cause a "Format" exception.
            '_datetimeTaken = Date.ParseExact(value, formats, CultureInfo.InvariantCulture, DateTimeStyles.None)

Date.blah doesn't work. dumbDate.blah works. strange.

your code:

DateTime datuMDokumenta = Convert.ToDateTime(txtDatumDokum.Text);

try changing this to:

DateTime datuMDokumenta = Convert.ToDateTime(txtDatumDokum);

and when u print the date/time

print datuMDokumenta.Text

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top