Back to Basics # 60: Get Users Local Date Time with Webresource in Dynamics CRM

Introduction:

In Dynamics 365 CRM, for certain requirements we need to get users local system date time to compare with the given dates for certain business logic. As an example, on selected contact record users local date time was fetched.

Step 1:

Login to the required environment and select required solution [Contact Customizations Solution in this case] as shown in the   below figure.

Step 2:

After Step 1, select contact web resource in solution and click on Edit as shown in the below figure.

Step 3:

After Step 2, below code to be used so as to convert UTC Time by providing offset and then extracting local date time of the machine where the user was accessing this record.

function  getdateInfo(executionContext)

{

    let formContext = executionContext.getFormContext();

    if (formContext !== null && formContext != ‘undefined’)

    {

           // Enter your offset

        let offset = 11;

            // retrieve Current Date

        let today = new Date();

            // convert to local time

        let  localTime = today.getTime();

            // obtain local UTC offset and convert to msec

        let localOffset = today.getTimezoneOffset() * 60000;

            // add local time to the offset of your machine

        let utc = localTime + localOffset;

            // obtain and add destination’s UTC time offset

        let ist = utc + (3600000*offset);

            // Convert local time to a new date

        let newdate = new Date(ist);

            // format dateTime

        let localdatetime = newdate.toISOString().slice(0, 19).replace(‘T’, ‘ ‘);

            console.log(localdatetime);

    }

}As shown in the below figure

Step 4:

After Step 3, final code looks like below

if (typeof (ContosoVaccination) == “undefined”)

{

    var ContosoVaccination = {__namespace: true};

}

if (typeof (ContosoVaccination.Scripts) == “undefined”)

{

    ContosoVaccination.Scripts = {__namespace: true};

}

ContosoVaccination.Scripts.ContactForm =

{

    handleOnLoad: function (executionContext)

        {

        console.log(‘on load – contact form’);

        getdateInfo(executionContext);

        },

    __namespace: true

}

function  getdateInfo(executionContext)

{

    let formContext = executionContext.getFormContext();

    if (formContext !== null && formContext != ‘undefined’)

    {

           // Enter your offset

        let offset = 11;

            // retrieve Current Date

        let today = new Date();

            // convert to local time

        let  localTime = today.getTime();

            // obtain local UTC offset and convert to msec

        let localOffset = today.getTimezoneOffset() * 60000;

            // add local time to the offset of your machine

        let utc = localTime + localOffset;

            // obtain and add destination’s UTC time offset

        let ist = utc + (3600000*offset);

            // Convert local time to a new date

        let newdate = new Date(ist);

            // format dateTime

        let localdatetime = newdate.toISOString().slice(0, 19).replace(‘T’, ‘ ‘);

            console.log(localdatetime);

    }

}

And as shown in the below figure.

Step 5:

After Step 4, save the code and publish the Webresource and open any contact record and console window in browser to see users local date time in his time zone as shown in the below figure.

Note:

  1. Make sure to publish all customizations and upload JavaScript (js) file.
  2. Make sure to use respective offset values.

Conclusion: In this way, one can easily get users local date time and use it to perform business logic like comparison of dates and other calculations based on dates using Webresource(javascript).

One thought on “Back to Basics # 60: Get Users Local Date Time with Webresource in Dynamics CRM

  1. Pingback: Back To Basics : Curated List of Articles in a Single Page – Common Man Tips for Power Platform, Dynamics CRM,Azure

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s