Get current user's date format for a Date Field

Posted by IramK on 11-Feb-2015 06:03

Hello,

I have a date field and I would like that to be formatted based on the current user's date settings. How can I achieve this? I tried getting the date format of the current user using {!CURR_USER[tag:dateFormat]} but its returning me the date itself rather than a format i.e. for eg: yyyy/mm/dd. How can I get the date format returned from the current user's date settings?

Posted by Orchid Corpin on 13-Feb-2015 16:38

Hi,

I can't think of way to dynamically get the format instead you can have it hardcoded based on its value.
On the event of the Date Format specify onchange function (object definition > Date Field > Event > onchange > getSelectedValue();)

After specify the code below. Kindly double check if each case corresponds with your dropdown format.

<script>
function getSelectedValue() {
  var dateFormat = rbf_getFieldValue("dateFormat");
  var format;
  console.log(dateFormat);
	switch (parseInt(dateFormat)) {
	    case 0:
	        format = "MM/dd/yyyy hh:mm tt";
	        break;
	    case 1:
	        format = "dd/MM/yyyy hh:mm";
	        break;
	    case 2:
	        format = "dd.mm.yyyy hh:mm";
	        break;
	    case 3:
	        format = "MM/dd/yyyy hh:mm";
	        break;
	    case 4:
	        format = "yyyy-MM-dd hh:mm";
	        break;
	    case 5:
	        format = "dd-MM-yyyy hh:mm";
	        break;
	    case 6:
	        format = "yyyy/MM/dd hh:mm";
	        break;
	    case 7:
	        format = "yyyy-MMM-dd hh:mm";
	        break;
	    case 8:
	        format = "dd/MM/yyyy hh:mm:s";
	        break;
	    
	}
	console.log(format);
}
</script>

Regards,
Orchid


Posted by Mohammed Siraj on 30-Sep-2015 07:26

This requirement has been addressed in Rollbase 4.0 release (New UI pages). Please review the code snippet below:

<script>

var rbf_getPageContext = function(){

 return rbf_isNewUI() ? rb.newui.page.PageContext : null;

};

try{

var pageContext = rbf_getPageContext ();

if(!Boolean(pageContext)){

throw "This script component is written specific to New UI Context.";

}

var locContext = pageContext .getPageLocalization();

rb.newui.util.logToConsole('Date format pattern -'+locContext.getDateFormat());

rb.newui.util.logToConsole('Date format (long) pattern -'+locContext.getLongDateFormat());

//how to format a date as per user's localization settings

rb.newui.util.logToConsole('Current date formatted as per users localization settings -'+

rbf_formatDate(new Date(),locContext.getLongDateFormat()));

//similarly for other formats

}

catch(err){

if(console){

console.log('Error executing script component in page. '+err);

}

}

</script>

All Replies

Posted by Orchid Corpin on 11-Feb-2015 10:35

Hi IranK,

You can use rbv_api.formatDate (for backkend) or rbf_formatDate (for front end). 

e.g.

var currentDate = new Date("new Date(rbv_api.getCurrentDate())");
    currentDate = rbv_api.formatDate(currentDate, "yyyy/MM/dd");
return currentDate;

I tried parsing the {!CURR_USER#dateFormat} to date value but seems to be not a valid date format so in the sample I get the current date in the Helper then use the rbv_api.formatDate

Hope this may help.

Regards,
Orchid

Posted by IramK on 12-Feb-2015 03:34

I think I did not explain my question properly. For a particular User, you have date format settings

I would like to get the format in which the date is displayed for that user i.e. for eg: yyyy-mm-dd HH:MM for the above user and format my date fields value it in the same format for that user. Users can have different date format settings and I would like to get their settings and display them the date in their own format. Any possible resolution to that?

Posted by Manooj Murali on 12-Feb-2015 03:49

We dont have a server or client side API which can take in a date and format it using user's date format pattern. It would probably be nice to have one.

Posted by IramK on 12-Feb-2015 05:15

Hello Manoj,

Thanks for your reply. We have customers that have US and UK date formats and we are setting the date field to have three months plus the current date. Now if current date for a user is lets say today in the format: 12-02-2015 - 12th of Feb, whilst for another user its like 2015-02-12 and so on. We are setting the field onload and because we are not catering for the user's date format its giving us an error and not allowing us to save the record. Is there a javascript/jquery method to cater for this instead?

Posted by Orchid Corpin on 13-Feb-2015 16:38

Hi,

I can't think of way to dynamically get the format instead you can have it hardcoded based on its value.
On the event of the Date Format specify onchange function (object definition > Date Field > Event > onchange > getSelectedValue();)

After specify the code below. Kindly double check if each case corresponds with your dropdown format.

<script>
function getSelectedValue() {
  var dateFormat = rbf_getFieldValue("dateFormat");
  var format;
  console.log(dateFormat);
	switch (parseInt(dateFormat)) {
	    case 0:
	        format = "MM/dd/yyyy hh:mm tt";
	        break;
	    case 1:
	        format = "dd/MM/yyyy hh:mm";
	        break;
	    case 2:
	        format = "dd.mm.yyyy hh:mm";
	        break;
	    case 3:
	        format = "MM/dd/yyyy hh:mm";
	        break;
	    case 4:
	        format = "yyyy-MM-dd hh:mm";
	        break;
	    case 5:
	        format = "dd-MM-yyyy hh:mm";
	        break;
	    case 6:
	        format = "yyyy/MM/dd hh:mm";
	        break;
	    case 7:
	        format = "yyyy-MMM-dd hh:mm";
	        break;
	    case 8:
	        format = "dd/MM/yyyy hh:mm:s";
	        break;
	    
	}
	console.log(format);
}
</script>

Regards,
Orchid


Posted by Meryk on 18-Feb-2015 08:09

Hi Orchid,

Thank you for you answer !

This code works fine for returning the current user format. So we added a script component with your function getSelectedValue in both pages "Edit User" (other users) and "Personal Setup" (current user) of object User, as well as a field we called "Correct Date Format", in which the format of teh date is stored.

Now we are just using the value in this field to format any dates in our application,

We still have an issue with the function "rbf_formatDate(date,format)", which does not seem to recognize this format :  "2015-Feb-18 12:52". I think only formats with numbers are recognized.

PS : When you change your date format (in settings user), you need to log out/ log in, before the format to be updated.

Thank you :)

Meryem

Posted by IramK on 19-Feb-2015 04:41

Hello Orchid & M,

I actually managed to get it working properly using a template field (dateFormatTemplate) with the above mentioned function but instead of using the rbf_dateFormat() to format the date, I've used a jquery ui function to do that. Possibly be useful for someone who might want to use this in the future.

jQueryUI date formatter: 
var dateTimeFormatted = $.datepicker.formatDate('{!#CURR_USER.dateFormatTemplate#value}', curDate);

Thanks for the function.

Posted by Orchid Corpin on 19-Feb-2015 09:30

Hi IramK,

Thank you for sharing this info. This is helpful to other users.

Regards,

Orchid

Posted by Mohammed Siraj on 30-Sep-2015 07:26

This requirement has been addressed in Rollbase 4.0 release (New UI pages). Please review the code snippet below:

<script>

var rbf_getPageContext = function(){

 return rbf_isNewUI() ? rb.newui.page.PageContext : null;

};

try{

var pageContext = rbf_getPageContext ();

if(!Boolean(pageContext)){

throw "This script component is written specific to New UI Context.";

}

var locContext = pageContext .getPageLocalization();

rb.newui.util.logToConsole('Date format pattern -'+locContext.getDateFormat());

rb.newui.util.logToConsole('Date format (long) pattern -'+locContext.getLongDateFormat());

//how to format a date as per user's localization settings

rb.newui.util.logToConsole('Current date formatted as per users localization settings -'+

rbf_formatDate(new Date(),locContext.getLongDateFormat()));

//similarly for other formats

}

catch(err){

if(console){

console.log('Error executing script component in page. '+err);

}

}

</script>

This thread is closed