Rakesh
Rakesh

Reputation: 2790

String to DateTime Convert Issue

Hi I have a string in following format 23/03/2014 and I have tried to convert it to this format:

string npacked = Convert.ToDateTime(packeddate).ToString("yyyy/MM/dd");

But I am getting an error:

String was not recognized as a valid DateTime

Also tried this:

string npacked = DateTime.Parse(packeddate).ToString("yyyy/MM/dd");

but same error.

Upvotes: 1

Views: 2645

Answers (4)

Soner Gönül
Soner Gönül

Reputation: 98868

Convert.ToDateTime(string) calls DateTime.Parse(string, CultureInfo.CurrentCulture) explicitly. That means your both lines are equivalent.

In your profile, it says you are from Dubai, United Arab Emirates. That's why I assume your CurrentCulture is probably ar-AE at first but your string matches it's ShortDatePattern and that's why it prints;

Convert.ToDateTime("23/03/2014", new CultureInfo("ar-AE")) // 23/03/2014

But you didn't tell us what is your CurrentCulture, we never know.. But looks like your current culture's date seperator is not / or your current culture doesn't have standart date format dd/MM/yyyy (which is unlikely for most of cultures) your both lines fail (first scenario is more likely).

You can easly parse your string with a culture that has / as a DateSeparator using DateTime.ParseExact method. This method let's you specify your custom date formats. We can use InvariantCulture for example;

var date = DateTime.ParseExact("23/03/2014",
                               "dd/MM/yyyy",
                               CultureInfo.InvariantCulture);

Now, let's consider to it's representation with yyyy/MM/dd format.

/ specifier has a special meaning of "replace me with the current culture's date separator" in custom date formats. That means if your date seperator is not / (I assume it is not) your date.ToString("yyyy/MM/dd") results will be included your date seperator, not /.

For example, I'm from Turkey, my culture is tr-TR. My date seperator is . (dot) That's why this example;

date.ToString("yyyy/MM/dd"); // Prints 2014.03.23 not 2014/03/23 

In such a case, you can use a culture which has / a date seperator as a second parameter in your ToString method (InvariantCulture for example) like;

date.ToString("yyyy/MM/dd", CultureInfo.InvariantCulture) // Prints 2014/03/23 

or you can escape your / character regardless which culture you use like;

date.ToString(@"yyyy\/MM\/dd") // Prints 2014/03/23 

Upvotes: 0

Damith
Damith

Reputation: 63105

try with ParseExact with the format

string npacked = DateTime.ParseExact(packeddate, "dd/MM/yyyy", CultureInfo.InvariantCulture).ToString("yyyy/MM/dd"); 

DEMO

Upvotes: 2

Suraj Shrestha
Suraj Shrestha

Reputation: 1808

It may be due to your system date time format. You have mm-dd-yyyy format in your system and you are trying to parse it in dd-mm-yyyy format. Try changing your system date format to dd/MM/yyyy.

Upvotes: 0

BradleyDotNET
BradleyDotNET

Reputation: 61379

Convert.ToDateTime is running a DateTime.Parse() on your string (23/03/2014). In the default culture (en-US) that is going to fail, since dates in that culture should be formatted MM/DD/YYYY. You need to switch to a different culture (like French) per MSDN:

  // Reverse month and day to conform to the fr-FR culture. 
  // The date is February 16, 2008, 12 hours, 15 minutes and 12 seconds.
  dateString = "16/02/2008 12:15:12";
  try {
     dateValue = DateTime.Parse(dateString);
     Console.WriteLine("'{0}' converted to {1}.", dateString, dateValue);
  }   
  catch (FormatException) {
     Console.WriteLine("Unable to convert '{0}'.", dateString);
  }

  // Call another overload of Parse to successfully convert string 
  // formatted according to conventions of fr-FR culture.       
  try {
     dateValue = DateTime.Parse(dateString, new CultureInfo("fr-FR", false));
     Console.WriteLine("'{0}' converted to {1}.", dateString, dateValue);
  }   
  catch (FormatException) {
     Console.WriteLine("Unable to convert '{0}'.", dateString);
  }

Calling "ToString" afterwards has no effect whatsoever on the parse attempt, it just formats the output of the parse.

Upvotes: 1

Related Questions