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 and time to compare with the given dates for certain business logic. As an example, on selected contact record users local the date and time it 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.

Get Users Local Date Time with Webresource in Dynamics CRM

Step 2

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

Get Users Local Date Time with Webresource in Dynamics CRM

Step 3

After Step 2, below code is 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);
    }
}

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);
    }
}

Step 5

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

Get Users Local Date Time with Webresource in Dynamics CRM

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 and time and use it to perform business logic like comparison of dates and other calculations based on dates using Webresource(javascript).


Similar Articles