开发者

DateTime FormatException error

开发者 https://www.devze.com 2023-04-03 05:28 出处:网络
DateTime datuMDokumenta = Convert.ToDateT开发者_JAVA技巧ime(txtDatumDokum.Text); txtDatumDokum.Text is like \"09.09.2011\".
DateTime datuMDokumenta = Convert.ToDateT开发者_JAVA技巧ime(txtDatumDokum.Text);

txtDatumDokum.Text is like "09.09.2011".

but i get FormatException error. Must i parse date?


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

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


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.


    public static void Main(string[] args)
    {
        var dt = new DateTime(2018, 04, 1);
        Console.WriteLine(dt);
       
        string month = dt.ToString("MMMM");
        Console.WriteLine(month);               //April

        month = dt.ToString("MMM");
        Console.WriteLine(month);              //Apr

        month = dt.ToString("MM");
        Console.WriteLine(month);             //04

        Console.ReadKey();
    }


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号