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)

Setting a DateTime field

(0) ShareShare
ReportReport
Posted on by 2,665

Hi,

I have a string variable which has a date value and I am trying to set the datetime field.

This way:

  string requestDate = (string)rowItem["Request Date"].ToString();
                            DateTime dt = Convert.ToDateTime(requestDate);
                            incident.Attributes["new_requestdate"] = dt;

This works fine if I have just the date value.. ex: rowItem["Request Date"]=9/20/2015

It is setting the field with default timestamp as 12:00:00 AM. No issues with this.

But it throws error when rowItem["Request Date"]= 9/14/2015 9:03:46 AM

How do I convert the string variable that is in two different formats and set them into datetime field?

Thanks for any help!

*This post is locked for comments

I have the same question (0)
  • Community Member Profile Picture
    on at

    Just use DateTime.Parse, or even better DateTime.ParseExact or the safe version DateTime.TryParseExact.

    Convert.ToDateTime is not the best approach to convert string to date.

  • Suggested answer
    Community Member Profile Picture
    on at

    Hi,

    Please use the below :

    DateTime dt =  

       DateTime.Parse(requestDate, System.Globalization.CultureInfo.InvariantCulture);  

  • Suggested answer
    razdynamics Profile Picture
    17,308 User Group Leader on at

    Hi Roxanna,

    Are you getting invalid date time error?

    Try below options.

    DateTime dt = DateTime.MinValue;

    dt = Convert.ToDateTime("9/14/2015 9:03:46 PM");

    DateTime newDate = new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second);

    OR

    // Create a DateTime from a STRING

    string dateString = "9/14/2015 9:03:46 AM";

    DateTime dt =

    DateTime.Parse(dateString, System.Globalization.CultureInfo.InvariantCulture);

    incident.Attributes["new_requestdate"] = dt;

    I Hope that helps, Kindly Verify :)

    Best Wishes, Raz

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