web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

DateTime ToString writing an API

(0) ShareShare
ReportReport
Posted on by 235

Hi all,

today i am writing an API that should receive a datetime format

public DateTime? DateOfBirth
{
get { return this.GetPropertyValue<DateTime?>("xxxx_dateofbirth"); }
set { this.SetPropertyValue<DateTime?>("xxxx_dateofbirth", value); }
}

and create a request.

That request has to be in string format. So i need to create the request by using the datetime value to string converted.

I tried

            request.DateOfBirth = Convert.ToString(DemographicsModification.DateOfBirth);

and also

request.DateOfBirth = DemographicsModification.DateOfBirth.ToString();

but i have always an exception error debugging, it says DateOfBirth value is not correct

There are some other ways to make it?

Thank you a lot

*This post is locked for comments

I have the same question (0)
  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    What are types of DemographicsModification.DateOfBirth and request.DateOfBirth?

  • Davide Cerqua Profile Picture
    235 on at

    DemographicsModification.DateOfBirth (DateTime)

    request.DateOfBirth (String)

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Ok. In that case request.DateOfBirth = DemographicsModification.DateOfBirth.ToString(); should work fine. Let's make next step what line of code raises an exception? Would be great to see full code you use and exception message/callstack e.t.c.

  • Community Member Profile Picture
    on at

    try

    DateTime dob = Convert.ToDateTime("xxxx_dateofbirth");

    assign dob to the field.

    If my response is helps you, please mark as "Verified"

  • Verified answer
    ChrisC Profile Picture
    630 on at

    As I'm sure you're aware dates are expressed differently in strings in different regions which is why the 'ticks' format is often used as its a whole number and invariant across cultures.

    Another invariant format would be the ISO8601 format e.g. 2007-04-05 which is pretty easy to format as a string.

    Perhaps you should consider using this format?

  • Verified answer
    Davide Cerqua Profile Picture
    235 on at

    Hello everyone,

    i solved this error so i am going to write my solution, maybe it will help someone else :)

               request.DateOfBirth = string.Format("{0}-{1}-{2}",

                   DateModification.DateOfBirth.GetValueOrDefault().Year.ToString(),

                   DateModification.DateOfBirth.GetValueOrDefault().Month.ToString(),

                   DateModification.DateOfBirth.GetValueOrDefault().Day.ToString()

    ChrisC you were right, infact i changed date format using -

    Thank you very much!

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans