Jump to content
  • 0

Overwrite Localization Date Format on datapage


DesiLogi

Question

My app has users in the USA who need the date format output displayed as mm/dd/yyyy (short & long date). But users in Australia, UK, etc need date format output to be dd/mm/yyyy (short & long date).

Problem  is there's only 1 option in a Localization for date format: either has to be mm/dd/yyyy OR dd/mm/yyyy and it cannot differentiate the user's location and switch them based on that.

I'm thinking that the Localization only provides the format on output and that it might be possible to overwrite or change that format using JS on the datapage itself, to switch the format if the Authorization field for the country is not the USA.

The basic logic would be something like this (I know it's not fully functional script, am just trying to show the thought process):

<script>
 
var v_country = parseFloat(document.getElementById("EditRecordAuthfieldMyCountryName”).value);


if (v_country == "United States"){
[@field:Date_Start].format("mmm dd yyyy");
}

else
if (v_country == "Australia"){
[@field:Date_Start].format("dd mmm yyyy");
}

</script>

One thing- I'm not quite sure how to reference an Authorization field in the GetElementbyID part. 

Does anyone know if it's possible to do this and if so exactly how the code would look. Many thanks for any help-

Link to comment
Share on other sites

7 answers to this question

Recommended Posts

  • 0

Hello DesiLogi,

What kind of Datapage do you use in described case?

Anyway, I have been using the following solution in my Tabular Report Datapage.

I have an HTML block which displays the date and consist of the following code:

<div id="demo[@cbRecordIndex#]"></div>

I have placed the following Javascript code into Footer to assign the date format based on authentication field:

<script>
var d = new Date('[@YOUR_DATE_FIELD*]'); //short format should be used
var options = { year: 'numeric', month: 'numeric', day: 'numeric' }; // output date only via digits

if ( '[@authfield:Country]' == 'Europe') {
var divs = document.getElementsByTagName("div"), item;
    for (var i = 0, len = divs.length; i < len; i++) {
    item = divs[i];
    if (item.id && item.id.indexOf("demo") == 0) {
        item.innerHTML = d.toLocaleString("en-GB", options); // outputs date in UK format
    }
  }
}
else {
var divs = document.getElementsByTagName("div"), item;
    for (var i = 0, len = divs.length; i < len; i++) {
    item = divs[i];
    if (item.id && item.id.indexOf("demo") == 0) {
        item.innerHTML = d.toLocaleString("en-US", options); // outputs date in US format
   }
 }
}
</script>

Hope this helps.

Link to comment
Share on other sites

  • 0

Hi Vitaliksssss,

Thanks for this code- I'm very much looking forward to trying to get it to work for my app. I'll need to use it on a lot of different kinds of datapages: tabular and detail especially. 

So far I can get it to read the authentication country and format the date differently. The problem is it is reading only the 1st record in the recordset and using that date for ALL results. It seems like it needs something sort of like target.[@field:myfield] to delineate records but I can't figure out how to put that in this code. This is what I have so far (I'm using 'Australia' for the Euro output):

<div id="demo[@field:Items_ItemID]"></div>

<script>
var d = new Date('[@field:PurchaseOrders_PO_Date*]'); //short format should be used
var options = { year: 'numeric', month: 'numeric', day: 'numeric' }; // output date only via digits

if ( "[@authfield:Company_Country]" == "Australia") {
var divs = document.getElementsByTagName("div"), item;
    for (var i = 0, len = divs.length; i < len; i++) {
    item = divs[i];
    if (item.id && item.id.indexOf("demo") == 0) {
        item.innerHTML = d.toLocaleString("en-GB", options); // outputs date in UK format
    }
  }
}
else {
var divs = document.getElementsByTagName("div"), item;
    for (var i = 0, len = divs.length; i < len; i++) {
    item = divs[i];
    if (item.id && item.id.indexOf("demo") == 0) {
        item.innerHTML = d.toLocaleString("en-US", options); // outputs date in US format
   }
 }
}
</script>

 

Link to comment
Share on other sites

  • 0

Any ideas on how to make this work? The date formatting works right but it's repeating the lowest value @field:Items_ItemID date for every record in the results. I can't figure out how to make the code reset for each record. Something to do with var= divs line I think. Any insight would be greatly appreciated. 

Link to comment
Share on other sites

  • 0

Hi DesiLogi,

I have been working on this issue for a while and came up a new JS solution as per your comment.

Please place the following code in Report Datapage Footer:

<script>
function convert_date(){
  
var col = 2; // please replace digit with YOUR_DATE column sequence number (starts from "0")

var options = { year: 'numeric', month: 'numeric', day: '2-digit' };

var cas_table = document.querySelectorAll('table[id^="cbTable"]')[0]; 

      var cas_rows = cas_table.getElementsByTagName("tr");
      for(var rowIndex=1; rowIndex < cas_rows.length; rowIndex++) 
      { 
         var cells = cas_rows[rowIndex].getElementsByTagName("td");
        
         v_rev = new Date(cells[col].innerHTML);
         
if ("[@authfield:Company_Country]" == "Australia") {
cells[col].innerHTML= v_rev.toLocaleString("en-GB", options); 
    }
 
else {
cells[col].innerHTML= v_rev.toLocaleString("en-US", options); 
      }
    }
}
convert_date();
</script>

 

Hope this helps.

 

Regards,

vitalikssssss

Link to comment
Share on other sites

  • 0

I was using the long date for this very purpose. This shorter date format you posted might work fine for you or me (or most users) but my clients in the USA would complain and then if I changed it to fit USA then UK/AUS would complain. I've got a calculated field solution that's almost finished that I think will solve the issue. I'll post that when it's ready. 

Link to comment
Share on other sites

  • 0

I got a workaround that doesn't use JS. In some ways this is quicker/easier to implement. Here it is if anyone wants it (thanks again for the above help- it's much appreciated). 

This is in a calculated field and references the 'Company_Country' authorization field to delineate which way to format the date. I have the 'Replace' clause there to remove the timestamp, which I don't want to show. 

CASE 
WHEN '[@authfield:Company_Country]' <> 'United States' Then
CONVERT(CHAR(10),[@field:MyDate],103)
ELSE
Replace([@field:MyDate],' 12:00AM','')
END

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...