1

I have an object, one of its propperty is DateTime DateofBirth. i get this object and want to change

{8/16/1978 12:00:00 AM}

to

{16/8/1978 12:00:00 AM}

.

DateTime? tmp = externalConsumerProfile.DateOfBirth;
string s=DateTime.ParseExact(tmp.ToString(), "MM/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture).ToString("dd/MM/yyyy hh:mm:ss tt") ;

But i have an error when try to convert

An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code

Additional information: String was not recognized as a valid DateTime.

When i try tmp.ToString("dd/MM/yyyy hh:mm:ss tt") z have an error

No overloads for method ToString

Евгений
  • 187
  • 3
  • 13
  • You have a `DateTime` object, and then try to parse it as a `DateTime` object – Jonesopolis Nov 23 '16 at 16:47
  • `Parse` converts a `string` to a `DateTime`. `tmp` IS already a `DateTime`, you don't need to make it one. Just format it. `var s = tmp.ToString("MM/dd/yyyy hh:mm:ss tt");` – Charles Bretana Nov 23 '16 at 16:47
  • Possible duplicate of [C# DateTime to "YYYYMMDDHHMMSS" format](http://stackoverflow.com/questions/3025361/c-sharp-datetime-to-yyyymmddhhmmss-format) – Kiogara Nov 23 '16 at 16:48
  • `tmp.ToString("dd/MM/yyyy hh:mm:ss tt");` – Pikoh Nov 23 '16 at 16:49
  • No overloads for method ToString – Евгений Nov 23 '16 at 16:57
  • `DateTime` doesn't _have_ a format. You specify the format when _displaying_ it, which is where you need to make the change. – D Stanley Nov 23 '16 at 17:05
  • You are right..it is a nullable DateTime, you need to use `tmp.Value.ToString("dd/MM/yyyy hh:mm:ss tt");` – Pikoh Nov 23 '16 at 17:06
  • How i can store changed `externalConsumerProfile.DateOfBirth = DateTime.ParseExact(dateOfBirths, "dd/MM/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);` I see that `externalConsumerProfile.DateOfBirth` still the same – Евгений Nov 23 '16 at 17:57

1 Answers1

3

All you need to do is format your DateTime value. You don't need to parse it.

DateTime? tmp = externalConsumerProfile.DateOfBirth;
string s=tmp.HasValue() ? tmp.Value.ToString("dd/MM/yyyy hh:mm:ss tt") : string.Empty;
Wagner DosAnjos
  • 6,304
  • 1
  • 15
  • 29