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

txtDatumDokum.Text is like "09.09.2011".

but i get FormatException error. Must i parse date?

AakashM
  • 62,551
  • 17
  • 151
  • 186
senzacionale
  • 20,448
  • 67
  • 204
  • 316

9 Answers9

3

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

 DateTime.ParseExact(txtDatumDokum.Text, "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None);
gyurisc
  • 11,234
  • 16
  • 68
  • 102
  • thx it works for me. So i must always use DateTime.ParseExact even if date is valid like dd.MM.yyyy – senzacionale Sep 08 '11 at 06:51
  • @senzacionale you don't have to use parseexact but it's recommendable if you are only ever going to received the dates in one specific format – Rune FS Sep 08 '11 at 06:57
  • Try using the correct culture for your area or de-DE if you aren't sure, that should work well without having to use ParseExact, see my answer. – crlanglois Sep 08 '11 at 07:15
1

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));
Marco
  • 56,740
  • 14
  • 129
  • 152
0

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.

BadBiki
  • 82
  • 4
0
    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();
    }
0

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.

Jon
  • 428,835
  • 81
  • 738
  • 806
  • becouse i use date.ToString("dd.MM.yyyy"). If i use ToShortDateString then i get 09/08/2011 which is not correct. Our date must be like 08.09.2011 – senzacionale Sep 08 '11 at 07:06
  • @senzacionale: If the question is about CF 3.5, please tag it as such. – Jon Sep 08 '11 at 07:11
0

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.

crlanglois
  • 3,537
  • 2
  • 14
  • 18
0

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
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
0

DateTime dt = Convert.ToDateTime(txtDatumDokum.Text)

It is right...there is no isssue

ankush
  • 1,051
  • 3
  • 19
  • 34
-3

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

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
A J
  • 1