1
votes

I have object that contains french date and i want to vert it to english date Exp: from 21/01/1990 00:00:00 to 1990-01-21 00:00:00 This my code to convert the date but didn't work because the output is 21/01/1990 00:00:00, meaning it didn't convert the date.

 if (obj.DATE_OPTIN.Equals("00/00/0000 00:00"))
      obj.DATE_OPTIN = Convert.ToDateTime(obj.DATE_OPTIN_ORGANISATEUR).ToString("yyyy'-'MM'-'dd'T'HH':'mm'");

Where DATE_OPTIN is a string value.

1
It's always helpful that as well as saying "it didn't work" that you show the output you got or the full text of any exception that occurred. - PaulF
Thanks for your response the output is 21/01/1990 00:00:00 that mean didn't convert the date - taniiit
DATE_OPTIN is a string - taniiit
Your conversion is conditional to the input matching exactly the string value "00/00/0000 00:00", which probably prevents the conversion from happening given any other string value. Did you mean for it to match a pattern instead? - Mathieu Guindon
My guess is that's the magic string that signals the date was not set. In that case, OP wants the property to be set to a culture-adjusted version of the other input (DATE_OPTIN_ORGANISATEUR) instead. This whole problem reeks of bad data modeling, though. - StriplingWarrior

1 Answers

3
votes

You should convert it to a DateTime first specifying it's a french format and then display it as an english data by specifying the culture in the DateTime.ToString method.

You should have something like this:

using System.Globalization;

var frenchDateString = "21/01/1990 00:00:00";

Console.WriteLine($"French format: {frenchDateString}");

var dateTime = DateTime.Parse(frenchDateString, new CultureInfo("fr-FR"));
var englishDateString = dateTime.ToString(new CultureInfo("en-EN"));

Console.WriteLine($"English format: {englishDateString}");

To adapt your code to any culture, you can check this to find your culture code: https://docs.microsoft.com/en-us/bingmaps/rest-services/common-parameters-and-types/supported-culture-codes