Get user personal settings: date & time format, date separator, number separator, decimal symbol, currency presision

Info:

If you have ever, I assume you must have, been annoyed with JS:

  • reading date/time format
  • reading date/time separator
  • reading currency precision
  • reading decimal symbol
  • reading decimal separator

don’t worry. You’re not alone.
Recently I’ve came across a problem of different date formats on grid view for users. Base setting has been overridden by users with their own formatting: File -> Options -> Formats.
One user has date format: yyyy/MM/dd with separator ‘-‘ while other has: d/MM/yyyy with separator ‘/’. How to get user settings? You must use UserSetting entity. It’s available for both JS and C# as a build-in entity. Loads of other attributes are available for this entity.

CrmVersion:

2011 OnPremises, RU15

Code:

UserSettingsHelper = {

    GetUserSettings: function (userId) {

        var attributes = "";
        
        for (var i = 1; i < arguments.length; i++) {
            attributes += '<attribute name="' + arguments[i] + '" />';
        }

        var fetchXml = [
            '<fetch mapping="logical">',
                '<entity name="usersettings">',
                    attributes,
                    '<filter>',
                        '<condition attribute="systemuserid" operator="eq" value="', userId, '" />',
                    '</filter>',
                '</entity>',
            '</fetch>'].join('');

        var result = XrmServiceToolkit.Soap.Fetch(fetchXml);

        return result.length > 0 ? result[0] : null;
    },

    GetCurrentUserDateTimeSettings: function(){

        var result = UserSettingsHelper.GetUserSettings(
                          Xrm.Page.context.getUserId(), 
                          "dateformatstring", 
                          "dateseparator", 
                          "timeformatstring", 
                          "timeseparator");

        return {
            DateSeparator: result.attributes.dateseparator.value,
            DateFormat: result.attributes.dateformatstring.value,
            TimeFormat: result.attributes.timeformatstring.value,
            TimeSeparator: result.attributes.timeseparator.value
        };
    },

    GetCurrentUseCurrencySettings: function () {

        var result = UserSettingsHelper.GetUserSettings(
                          Xrm.Page.context.getUserId(), 
                         "numberseparator", 
                         "decimalsymbol", 
                         "currencydecimalprecision", 
                         "numbergroupformat");

        return {
            NumberSeparator: result.attributes.numberseparator.value,
            DecimalSymbol: result.attributes.decimalsymbol.value,
            CurrencyDecimalPrecision: result.attributes.currencydecimalprecision.value,
            NumberGroupFormat: result.attributes.numbergroupformat.value
        };
    }
}

2 thoughts on “Get user personal settings: date & time format, date separator, number separator, decimal symbol, currency presision”

    1. What do you mean ‘overwritten’?? Only for displaying purposes or saving new values in DB?

Have an own opinion?