Geeks With Blogs
$håùrÿá @ñäñd "Imagination is more important than knowledge."

It is sometime necessary to convert CrmDateTime type field to DateTime type field and vice versa so that the date and time operations are done swiftly. I happen to have felt this necessity today and wrote this function that converts DateTime To CrmDateTime:

   1:  static public CrmDateTime ConvertToCRMDateTime(DateTime dateTime)
   2:  {
   3:      CrmDateTime crmDateTime = new CrmDateTime();
   4:      crmDateTime.date = dateTime.ToShortDateString();
   5:      crmDateTime.time = dateTime.ToShortTimeString();
   6:      TimeSpan offset = TimeZone.CurrentTimeZone.GetUtcOffset(dateTime);
   7:      string sOffset = string.Empty;
   8:      if (offset.Hours < 0)
   9:      {
  10:          sOffset = "-" + (offset.Hours * -1).ToString().PadLeft(2, '0');
  11:      }
  12:      else
  13:      {
  14:          sOffset = "+" + offset.Hours.ToString().PadLeft(2, '0');
  15:      }
  16:      sOffset += offset.Minutes.ToString().PadLeft(2, '0');
  17:      crmDateTime.Value = dateTime.ToString(string.Format("yyyy-MM-ddTHH:mm:ss{0}", sOffset));
  18:      return crmDateTime;
  19:  }        

To get DateTime from CrmDateTime, one can use the Convert.ToDateTime method:

   1:  DateTime tempNextDeliveryDate = CalculateNextDeliveryDate(Convert.ToDateTime(acc.ace_datumletztelieferung.date), acc.ace_lieferzyklus.Value);

To learn about how CrmDateTime stores the date & time refer: http://msdn2.microsoft.com/en-us/library/aa613549.aspx

Posted on Friday, July 27, 2007 5:30 PM CRM | Back to top


Comments on this post: Converting DateTime to CrmDateTime

# re: Converting DateTime to CrmDateTime
Requesting Gravatar...
Hello Shaurya,
Thanks for this code. I used it and found out that a symbol is missing in Offset conversion.
Acutally offset has to be in ##:## format...but your code is missing ":" ..so it gives offset in #### format..
Which has caused timezone differences in my crm...after putting ":", it worked fine..
Good Work!
Left by Manpreet Grewal on Feb 15, 2008 8:06 AM

# re: Converting DateTime to CrmDateTime
Requesting Gravatar...
Thanks for this code example.
I'm implementing a contract auto-renewal feature and experiencing errors when I go to execute the update as follows:

"Start date / end date or billing start date / billing end date is invalid."

I've installed both the Rollup 2 and 3.
I've also used this conversion function without further success.
Left by Corbin Plouzek on May 21, 2008 2:59 AM

# re: Converting DateTime to CrmDateTime
Requesting Gravatar...
Hi, From this snippet i found the idea to use Convert.ToDateTime Method when you want to convert CRMdatetime to DateTime.

Thanks,
Digisha
Left by Digisha Modi on Sep 01, 2008 3:57 AM

# re: Converting DateTime to CrmDateTime
Requesting Gravatar...
Try using the z character to obtain the utc offset.

http://msdn.microsoft.com/en-us/library/8kb3ddd4(VS.71).aspx
Left by n/a on Jan 06, 2009 2:04 AM

# re: Converting DateTime to CrmDateTime
Requesting Gravatar...
Thanks heaps for this post
Left by Dineshn on Jan 19, 2011 12:48 AM

# re: Converting DateTime to CrmDateTime
Requesting Gravatar...
here is a simplification that I've seen to translate datetime to Value

crmDateTime.Value = dt.ToString("s");
Left by Don Rule on Sep 28, 2011 12:46 AM

Your comment:
 (will show your gravatar)


Copyright © Shaurya Anand | Powered by: GeeksWithBlogs.net