How to get the current datetime in CRM client side using Javascript.

I got the issue where I needed to get the datetime as per CRM logged in user timezone. I have written one more blog where we can do this at server side by hitting the org service Get date time as per current user time zone , but how to do that at client side??

Either we can hit the CRM services to get the timezone and calculate the time or we have one simple way to do that as below:

What exactly below code does?

This gets the current machine time and then converts the UTC datetime. after that we calculate crm user datetime by adding the current time zone offset minutes of logged in user.

How to get the offset minutes of the login user?

Xrm.Page.context.getTimeZoneOffsetMinutes();

////Please note: This method is available only after “CRM 2015 Update 1” version.

over all code is as below:

var date = new Date();

var utcDate = new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds(), date.getUTCMilliseconds());

var crmUserDateTime = new Date(utcDate.setMinutes(utcDate.getMinutes() + Xrm.Page.context.getTimeZoneOffsetMinutes()));

We can ever use jQuery date picker and simply pass the date in var date = new Date(“05/25/2017”);

var date = new Date(“05/25/2017”); // here we can pass multiple formated dates. and any date.

var utcDate = new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds(), date.getUTCMilliseconds());

var crmUserDateTime = new Date(utcDate.setMinutes(utcDate.getMinutes() + Xrm.Page.context.getTimeZoneOffsetMinutes()));

Hope this helps 🙂

Happy learning.

Please follow my blog, so that whenever i create any blog you get notifications.

SSRS Report in CRM 2015 Date time of the user’s time zone

for showing the user’s zone, date time in the CRM 2015.

  1. first of all add the parameter to the report “CRM_UserTimeZoneName
  2. New Bitmap Image
  3. New Bitmap Image
  4. Add the textbox in the report and edit expression in it , write below expression in it as belowNew Bitmap Image

=CDate(Microsoft.Crm.Reporting.RdlHelper.DateTimeUtility.ConvertUtcToLocalTime(DateTime.UtcNow, Parameters!CRM_UserTimeZoneName.Value))

5. Right click on report(not on the body or report) and go to report property “references”

click on add or remove assemblies: Add button

paste this assembly reference as below

Microsoft.Crm.Reporting.RdlHelper, Version=7.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35

New Bitmap Image

Initially when you run this report in the visual studio then the date time textbox might show as “Error#”, but once you upload this on CRM 2015 , you can see the correct time zone will appear in the text box.

And finally you are done..

Happy learning.. 🙂