Quantcast
Viewing all articles
Browse latest Browse all 3363

How to Read and Set Date Format of Logged-in User in PCF Control

Introduction

Recently in our PCF control, we got the requirement that when the user selects a date from the calendar control then that date format should be of logged-in user. In this blog, we will check how to achieve the same.

Scenario:

Here is my current user settings in Dynamics 365 CRM.

Image may be NSFW.
Clik here to view.
ReadAndSetDateFormat

As you can see here, the date format are as follows:

Short date: yyyy/MM/dd

Long date: MMMM,d,yyyy

In this example, I will be retrieving short date format.

Example

Here is the pop-up window in which I want to retrieve some basic user information including their joining date.

Image may be NSFW.
Clik here to view.
ReadAndSetDateFormat

For Joining date field, I have used fabric UI datepicker control. Now when the user selects a date from datepicker like below then I want to set the date in the format of current logged-in user.

Image may be NSFW.
Clik here to view.
ReadAndSetDateFormat

Get the Date Format

To get the logged-in user date format settings, I have retrieved dateFormattingInfo from PCF context object as below:

Context.userSettings.dateFormattingInfo.shortDatePattern. //For Short Date Format.

Context.userSettings.dateFormattingInfo.longDatePattern. //For Long Date Format.

Set the date in datepicker control

To set the retrieved date format to the datepicker control, I have used formatDate property of datepicker control.

Image may be NSFW.
Clik here to view.
ReadAndSetDateFormat

Here is the function to set the user selected date:

Image may be NSFW.
Clik here to view.
ReadAndSetDateFormat

Result

As you can see in the below image, I was able to set the date in logged-in user’s format(yyyy/MM/dd )

Image may be NSFW.
Clik here to view.
ReadAndSetDateFormat

Conclusion

With the help of Context.userSettings.dateFormattingInfo.shortDatePattern, we can read and set date from logged-in user’s date format.

Image may be NSFW.
Clik here to view.


Viewing all articles
Browse latest Browse all 3363

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>