Views:

Overview

This article will walk you through some scenarios regarding setting date fields when using the CreateRecord() & UpdateRecord() functions.

Assumptions

  • Dynamics CRM stores all date \ datetime fields in the database in UTC timezone
  • When using the functions CreateRecord() or UpdateRecord() it is always assumed that the incoming date is in the UTC timezone.
  • For the purposes of this article we will assume the user has their timezone set to "GMT -05:00 Eastern Time". This can be found under the user settings within Dynamics CRM.

Example 1 : 

UpdateRecord('contact',
                          [contact.contactid],
                         SetAttribute('birthdate', UtcDateTime())
                        )

Comments :

-The UtcDateTime() function returns the current date & time for the UTC timezone so this could be "2013-Jan-10 18:23:45". It is this datetime which will be stored in the database.

-As the user is in a timezone -5 hours on UTC time the user will see in the Birth Date field on the form the following datetime, "2013-Jan-10 13:23:45". Notice a completely different time.

Example 2 : 

UpdateRecord('contact',
                          [contact.contactid],
                         SetAttribute('birthdate', UtcDate())
                        )

Comments :

-The UtcDate() function returns the current date for the UTC timezone & always sets the time component to midnight.  so this could be "2013-Jan-10 12:00:00". It is this datetime which will be stored in the database.

-As the user is in a timezone -5 hours on UTC time the user will see in the Birth Date field on the form the following datetime, "2013-Jan-09 19:23:45". Notice a completely different date and time.

Note 1:
In most cases it is best to use UtcDateTime() when you need to set a date field to today.

Note 2:
Date & Time settings can be tricky to understand within Dynamics CRM. If you really want to get up to speed on how they work please read this excellent article from Scott Durow of Ribbon WorkBench fame. 

http://www.develop1.net/public/post/2011/12/06/Dynamics-CRM-DateTimes-the-last-word

Comments (3)
  • When needing to output dates as a string within a N52 formula it might be useful to know that you can append "_fmt" to your date-attribute name to get a nicely formatted (non-US!!) date to use in text fields, prompts and error messages. For example:
    ​​​​​​OutputToTrace("Account was originally created on " + [account.createdon_fmt])​  /* Outputs using locale format of executing user */
  • Update link for Develop1 article on Datetimes: http://www.develop1.net/public/post/2011/12/06/Dynamics-CRM-DateTimes-the-last-word
  • Thanks Greg, the link has been updated.