Skip to main content

Notifications

Announcements

No record found.

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 129

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

  • Suggested answer
    Iron Arm Profile Picture
    Iron Arm 129 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
    Iron Arm 129 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
    Iron Arm 129 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
    Iron Arm 129 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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,430 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans