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

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Set date from string with custom pattern in C# plugin

(0) ShareShare
ReportReport
Posted on by 131

I'm trying to set a date field from a C# plugin. I have a string, and a custom date pattern. The date is always showing up in UTC time instead of EST.

I've tried using EST in my dateString and format. I've tried converting to UTC using DateTimeStyles.AdjustToUniversal and ToUniversalTime().  I've tried using DateTimeOffset instead of DateTime. 

I know that I need to adjust my dateString before I set the field but not sure how. 

The following code results in a time of 12:20 PM.  Which makes sense because the current offset for UTC is -4

string dateString = "2019-12-30 4:20 PM";
string format = "yyyy-MM-dd h:mm tt";
try
{
DateTime myDate = DateTime.ParseExact(dateString, format, System.Globalization.CultureInfo.InvariantCulture, DateTimeStyles.None);
entity["field"] = myDate;
}
catch
{
throw new InvalidPluginExecutionException(dateString + " is an invalid date format");
}

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Iron Arm Profile Picture
    131 on at
    RE: Set date from string with custom pattern in C# plugin

    Got it working.  You have to convert from the time zone to UTC

    TimeZoneInfo est = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");

    DateTime newTime = TimeZoneInfo.ConvertTimeToUtc(myDate, est);

  • Iron Arm Profile Picture
    131 on at
    RE: Set date from string with custom pattern in C# plugin

    I lied.  Doesn't work.  My plugin fires post operation on email create.  Funny, I made the plugin fire on the "assign" event for troubleshooting and it works.  It also works if I set the time to local time in the plugin, but that's not best practice since everything is stored in UTC

  • Iron Arm Profile Picture
    131 on at
    RE: Set date from string with custom pattern in C# plugin

    Got it working

    DateTime myDate = Convert.ToDateTime(dateString).ToLocalTime().ToUniversalTime();

  • Iron Arm Profile Picture
    131 on at
    RE: Set date from string with custom pattern in C# plugin

    I think I know what's going on.  That code is setting the time in UTC, and when page renders in CRM it converts it to my timezone (EST).  So I need to convert to local time before setting it.

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…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Aric Levin - MVP Profile Picture

Aric Levin - MVP 2 Moderator

#2
MA-04060624-0 Profile Picture

MA-04060624-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans