0

I am trying to convert a string value into DateTime. It gives me an error as, specified string is not in correct format.

Here is the code,

DateTime myDate = DateTime.ParseExact("07-09-2013 01:14:14:1414", "yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
string strDate = "07/09/2013 01:04:02:4";
Convert.ToDateTime(strDate); 
DateTime dt = DateTime.Parse(strDate);

Please help in converting the same.

Thanks

3
  • 2
    Never heard of this exception. Commented Jul 9, 2013 at 8:00
  • @TimSchmelter Oh stop it, you. Commented Jul 9, 2013 at 8:00
  • My answer won't work in your case? stackoverflow.com/a/17543140/447156 Commented Jul 9, 2013 at 8:27

5 Answers 5

2

Your format seems to be incorrect. Should be:

"dd-MM-yyyy HH:mm:ss:ffff"

Update. If number of digits representing fractions of a second varies, than the best bet is

"dd-MM-yyyy HH:mm:ss:FFFFFFF"

Refer to MSDN for other options for custom time and date formats.

Sign up to request clarification or add additional context in comments.

13 Comments

Are you sure this works, guess for invariant the millisecond part needs a decimal point like ss.ff
1414 suggests 4 characters, which will be 'ffff'. See msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
@Andrei This worked, with appended AM/PM. How can I remove AM/PM
@VijayBalkawade, there is no AM/PM in the original string, so it is not clear what you mean by appended AM/PM. However you always can add tt to the format to take those into account: dd-MM-yyyy HH:mm:ss:ffff tt should successfully parse 07-09-2013 01:14:14:1414 AM.
@Andrei The output is 9/7/2013 1:14:14 AM. I want to remove the AM or PM.
|
1

Try in your page_load event:

Thread.CurrentThread.CurrentUICulture = New System.Globalization.CultureInfo("tr-TR")
Thread.CurrentThread.CurrentCulture = New System.Globalization.CultureInfo("tr-TR")

4 Comments

Page_Load? Why? And why do you force him to use Turkish culture. That's pointless.
Page_Load to see if his problem is culture specific. Turkish culture because as far as I can understand, he is trying to convert a date to a dd/MM/yyyy format.. I only suggested a quick test not an actual solution to the case itself hence the word "Try" in the beginning of my anwer.
surely you would put this into a comment then, because this field is retained for answers....
@Emin First of all, this could be a console application or a winforms application too. You can't be sure about that. Second of all, OP used InvariantCulture in his code, so he wants to parse it culture-independent. There is no point to suggest him to use a specific culture.
1

First, you mixed you years place

07-09-2013 is dd-MM-yyyy format

second, you need a :ffff after seconds

So the final line should be

DateTime myDate = DateTime.ParseExact("2013-07-09 01:14:14:1414", "yyyy-MM-dd HH:mm:ss:ffff", System.Globalization.CultureInfo.InvariantCulture);

Comments

1

Your format isn't correct: "07-09-2013 01:14:14:1414" "yyyy-MM-dd HH:mm:ss"

Atleast your date is the other way around, and the milliseconds is not specified. Correct you format according to this: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

For the downvoter: The correct format is specified by Andrei: "dd-MM-yyyy HH:mm:ss:ffff"

2 Comments

Yup, someone is downvoting you but you are right. However you should be more clear
Sigh indeed, I was trying to let the Vijay understand why it was wrong. The correct format is posted by Andrei.
1

First of all, you should change your date format to dd-MM-yyyy because it fits with your date format.

Second of all, for miliseconds part, you can use . instead of :

DateTime myDate = DateTime.ParseExact("07-09-2013 01:14:14.1414", "dd-MM-yyyy HH:mm:ss.ffff", System.Globalization.CultureInfo.InvariantCulture);
string strDate = "07/09/2013 01:04:02.4";
Convert.ToDateTime(strDate);
DateTime dt = DateTime.Parse(strDate);

Here a DEMO.

For more information, check Custom Date and Time Format Strings

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.