-3

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.

Mathieu Guindon
  • 69,817
  • 8
  • 107
  • 235
taniiit
  • 41
  • 5
  • 3
    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 Jul 28 '22 at 15:43
  • Thanks for your response the output is 21/01/1990 00:00:00 that mean didn't convert the date – taniiit Jul 28 '22 at 15:46
  • What type is `DATE_OPTIN`? – ProgrammingLlama Jul 28 '22 at 15:48
  • 1
    DATE_OPTIN is a string – taniiit Jul 28 '22 at 15:49
  • 1
    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 Jul 28 '22 at 15:52
  • 1
    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 Jul 28 '22 at 16:00
  • yes i wanted to match it with a specfic pattern – taniiit Jul 28 '22 at 16:01
  • string.Equals is the wrong tool for that. You could use a regex, but really you want to get an actual `DateTime` value as early as possible and avoid doing string manipulations for this. How about `if (DateTime.TryParseExact(...))` instead? Then you get a `DateTime` and you can call `.ToString` to format it as per your needs. – Mathieu Guindon Jul 28 '22 at 16:22
  • The answer below is the best way to go - but when I tested your code I got an exception "Cannot find a matching quote character for the character '''." - there is an extra single quote after mm - once removed the output is "1990-01-21T00:00" - to get what you asked for use ".ToString("yyyy-MM-dd HH:mm:ss");" – PaulF Jul 28 '22 at 16:37

1 Answers1

5

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://learn.microsoft.com/en-us/bingmaps/rest-services/common-parameters-and-types/supported-culture-codes

yguerin
  • 196
  • 1
  • 6
  • Thanks but the output is 1/21/1990 12:00:00 AM and what i want is "01-21-1990 12:00:00" – taniiit Jul 28 '22 at 15:59
  • 4
    Then you need specify a custom format. The output displayed here is the default english culture. You can take a look at the following documentation: https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings – yguerin Jul 28 '22 at 16:18
  • @taniiit once you have a `DateTime` value for the input (use `DateTime.TryParseExact` to conditionally match a given pattern/format), just use `.ToString("dd-MM-yyyy hh:mm:ss")` and you're good to go. – Mathieu Guindon Jul 28 '22 at 16:43