5

I am developing windows application.

In that i have date in the string format as>> fileDate="15/03/2013"

I want it to be get converted into date format as my database field is datetime.

I used following things for it>>

DateTime dt = DateTime.ParseExact(fileDate, "yyyyy-DD-MM", CultureInfo.InvariantCulture);

DateTime dt = DateTime.Parse(fileDate);

Both of these methods proved failure giving me error>>

String was not recognized as a valid DateTime.

What can be mistake?

Is there another technique to do that?

6 Answers6

6
 string fileDate = "15/03/2013";
 DateTime dt = DateTime.ParseExact(fileDate, "dd/mm/yyyy", CultureInfo.InvariantCulture);
Frank59
  • 3,141
  • 4
  • 31
  • 53
  • There is one problem>> my fileDate has value "15/03/2013" and dt [after conversion] is getting me to value as > 1/15/2013 12:03:00 AM. What can be the issue? –  Mar 19 '13 at 06:50
  • 3
    Change dd/mm/yyyy by dd/MM/yyyy. mm (in lower case) is the format string for minutes, not for months. – HuorSwords Mar 19 '13 at 06:56
4

You have to give the date format according to the date string you have to ParseExact. You can see more on Custom DateTime format - MSDN

Change

"yyyy-MM-dd HH:ss"

To

"dd/MM/yyyy"

Your code would be

DateTime dt = DateTime.ParseExact(fileDate, "dd/MM/yyyy", CultureInfo.InvariantCulture);
Adil
  • 146,340
  • 25
  • 209
  • 204
  • 1
    Date separator is `/` not `-` ! – Harsh Baid Mar 19 '13 at 06:42
  • @Adil There is one problem>> my fileDate has value "15/03/2013" and dt [after conversion] is getting me to value as > 1/15/2013 12:03:00 AM. What can be the issue? –  Mar 19 '13 at 06:52
  • It gives 15/03/2013 12:00:00 AM, make sure you use the correct format string i.e. "dd/MM/yyyy" – Adil Mar 19 '13 at 07:00
  • @PakkPuneriPorag You must use capital `"MM"` or `"M"` for _months_, not `"mm"` or `"m"` which mean _minutes_. The `3` has become minutes in your example. – Jeppe Stig Nielsen Mar 19 '13 at 07:01
2

You should do this:

DateTime dt = DateTime.ParseExact(fileDate, "dd/MM/yyyy",CultureInfo.InvariantCulture);

You must pass in the string for the format ("dd/MM/yyyy") in the same style that you pass in the string fileDate.

jordanhill123
  • 4,142
  • 2
  • 31
  • 40
2

u may try with this SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy"); Date convertedDate = dateFormat.parse("ur_dateString")

  • Yeah it got worked but, There is one problem>> my fileDate has value "15/03/2013" and dt [after conversion] is getting me to value as > 1/15/2013 12:03:00 AM. What can be the issue? –  Mar 19 '13 at 06:51
  • The order in the original question is `"dd/MM/yyyy"`, not `"MM/dd/yyyy"`. And the `SimpleDateFormat` type is for Java, I think, not C#. – Jeppe Stig Nielsen Mar 19 '13 at 07:07
1

In your current code you are using format "yyyyy-DD-MM" which is wrong since date part require lower case d not upper case D. , Also for year part you are specifying 5 ys, it should be 4, like yyyy, the order according to your date string should be: "dd/MM/yyyy". To be on the safe side you can even use "d/M/yyyy", which would work for single digit or double digit day/month.

So your code should be:

string fileDate="15/03/2013";
DateTime dt = DateTime.ParseExact(fileDate, "dd/MM/yyyy", CultureInfo.InvariantCulture);

You can see more on Custom DateTime format - MSDN

Habib
  • 219,104
  • 29
  • 407
  • 436
  • There is one problem>> my fileDate has value "15/03/2013" and dt [after conversion] is getting me to value as > 1/15/2013 12:03:00 AM. What can be the issue? –  Mar 19 '13 at 06:53
  • @PakkPuneriPorag, this seems pretty weird, are you certain that your fileDate has the above string, also make sure you are not doing `DateTime.Parse` like in the question. – Habib Mar 19 '13 at 06:57
  • By mistake i used mm instead of MM –  Mar 19 '13 at 06:59
  • 1
    `mm` is for minutes, whereas `MM` is for month. – Habib Mar 19 '13 at 06:59
  • Yeah, i came to know that.Thanks for support –  Mar 19 '13 at 06:59
0

It's because string "15/03/2013" cannot really be parsed as DateTime with format string "yyyy-MM-dd HH:ss".

Axarydax
  • 16,353
  • 21
  • 92
  • 151
  • i refered this http://stackoverflow.com/questions/919244/converting-string-to-datetime-c-net that why i put that –  Mar 19 '13 at 06:36