Hi All,

I have an xml which I am reading in a date:

  <DateOfBirthUpdate>
    <DateOfBirth>1951-04-25</DateOfBirth>
  </DateOfBirthUpdate>

My code reads this in as 25/4/1951 00:00:00 but I need to convert it back to it's original format of 1951-04-25 so it can be used in a soap message.

dateOfBirthUpdate.DateOfBirth = Convert.ToDateTime(record.Element("DateOfBirthUpdate").Element("DateOfBirth").Value);

inUpdate.DateOfBirthUpdate = dateOfBirthUpdate;  

I have tried various methods but nothing seems to work.

Many thanks inadvance..

Have you had a look at this ?

Yup, but that implies I have to convert it from a date format to a string then convert it back.

When the code hits inUpdate.DateOfBirthUpdate = dateOfBirthUpdate, its the dateOfBirthUpdate that has to be in Date type with it converted to yyyy-MM-dd

You could also use DateTime.Parse(...)

Yup, but that implies I have to convert it from a date format to a string then convert it back.

Nope, you just need to convert it to a string since you already have a valid DateTime object:

string formattedDate = dateOfBirthUpdate.DateOfBirth.ToString("yyyy-MM-dd");    

ddanbe's suggestion works well I use it at the moment, plus if it stays are a DT format when you store it in a string it's easily converted back to DT type.

commented: Well said! :) +14
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.