How to handle UTC Daylight saving using Javascript in MSCRM.


Call the function Now from the onload event or change event. The current function will populate the current date and time on your date time field and will also take care of the daylight savings.

Capture 24

function Now(primaryControl, attributename) {
    debugger;
    var formcontext = primaryControl.getFormContext(); //formcontext is new
    // upgraded way of 
    //calling Xrm.Page which now 
    // got depricated in D365
    //If you are still working on crm 16 or below you can directly use Xrm.Page 
    if (formcontext.ui.getFormType() == 1) {
        var date = new Date();
        if (date.dst()) {
            date.setHours(date.getHours() + 1);
            formcontext.getAttribute(attributename).setValue(date);
            return;
        }
        formcontext.getAttribute(attributename).setValue(date);
    }
}

Date.prototype.stdTimezoneOffset = function () {
    debugger;
    var jan = new Date(this.getFullYear(), 0, 1);
    var jul = new Date(this.getFullYear(), 6, 1);
    return Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset());
}

Date.prototype.dst = function () {
    debugger;
    return this.getTimezoneOffset() < this.stdTimezoneOffset();
}

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