String was not recognized as a valid DateTime?
Asked Answered
V

4

3

I try to convert string to datetime but each time I get :

String was not recognized as a valid DateTime.

Code is:

string format = "dd/MM/yyyy";

obj.TransDate = DateTime.ParseExact(lbl_TransDate.Text.Split('/')[0] + "/" + lbl_TransDate.Text.Split('/')[1] + "/" + lbl_TransDate.Text.Split('/')[2], format, CultureInfo.InvariantCulture);

When I debug the date which I try to parse it is : 12/4/2012

Valerio answered 12/4, 2012 at 12:0 Comment(3)
out of plain curiosity, why not just DateTime.ParseExact(lbl_TransDate.Text, format, CultureInfo.InvariantCulture)?Blueberry
:) it is not my code , just i test this partValerio
Did you check it with this code DateTime dt; string Temp1 = "Your Date"; if (DateTime.TryParse(Temp1, out dt)) { // If it is a valid date string date = dt.ToShortDateString(); string time = dt.ToShortTimeString(); } Fizgig
M
9

The desired format is

string format = "dd/M/yyyy";

I don't understand a thing though, why split an concatenate the string, since you would obtain the same thing?

If the input is 12/4/2012, after the split by '/', you'll get 12, 4, 2012 and then concatenate them back to obtain "12/4/2012". Why this?

Also, if you really need that split, you can store in into an array so you don't need to split it 3 times:

var splits = lbl_TransDate.Text.Split('/');
DateTime.ParseExact(splits[0] + "/" + splits[1] + "/" + splits[2], ...);

If you don't trust the input, the splits array might not be of Length = 3, and more of it, you can use DateTime.TryParseExact

EDIT You can use the overload with multiple formats So if the input might be 12/4/2012 or 12/04/2012, you can give both formats

var formats = new[] {"dd/M/yyyy","dd/MM/yyyy"};
var date = DateTime.ParseExact("12/4/2012", formats, 
                                        System.Globalization.CultureInfo.InvariantCulture,
                                        System.Globalization.DateTimeStyles.AssumeLocal);
Monzonite answered 12/4, 2012 at 12:2 Comment(2)
the informix date "dd/MM/yyyy".Valerio
The DateTime represented by the string is not supported in calendar System.Globalization.GregorianCalendar. when i try yours.Valerio
W
3

I agree with the other answers that it looks like your doing a lot to solve what should be a simple problem. I'd use the British date format from the culture info.

var convertedDay = DateTime.Parse("12/04/2010", new CultureInfo("en-GB").DateTimeFormat);
Winston answered 12/4, 2012 at 12:18 Comment(0)
R
1

You are specifying MM when you only have a single digit. Either use just a single M, or pad left with zero using the PadLeft function.

The following code demonstrates this with both dd and MM padded as desired

string format = "dd/MM/yyyy";
string mydate = "12/4/2012";
DateTime t = DateTime.ParseExact(mydate.Split('/')[0].PadLeft(2,'0') + "/" + 
                                 mydate.Split('/')[1].PadLeft(2,'0') + "/" + 
                                mydate.Split('/')[2], format, CultureInfo.InvariantCulture);

Output is:

12/04/2012 00:00:00
Rhetor answered 12/4, 2012 at 12:6 Comment(0)
B
1

Make sure you add the following in your web config

<system.web>
<globalization culture="en-AU"/>...

See the following for the correct culture country code

http://sharpertutorials.com/list-of-culture-codes/

Byre answered 11/9, 2012 at 6:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.