2

Dates have a different format by country. In Sharepoint 2013, there is a form with a date field and I need to display this date based on the cultural information of each user.

Based on other discussions here, there is no way to get this piece of information through js COM.

An alternative is to extract the office location of each user by loading the PersonProperties and customize the date accordingly but this is not a solid solution.

Do you have any other suggestion?

Thanks in advance.

2

You can get the date format from regional settings:

var ctx = new SP.ClientContext(siteUrl);
var web = ctx.get_web();
ctx.load(web);
var regionalSettings = web.get_regionalSettings();
ctx.load(regionalSettings);

ctx.executeQueryAsync(
    function(sender,args) 
    {
        console.log(regionalSettings.get_dateFormat());
        console.log(regionalSettings.get_dateSeparator());
    },
    function(sender,args)
    {
        console.log(args.get_message());
    }
);

The resulting date format is an integer: 0 (MMddyyyy), 1 (ddMMyyyy) or 2 (yyyyMMdd).

Tested the code on English US and English UK regional settings and it works fine.

0

The solution I use for my library (SharepointPlus) is to load the page /_layouts/regionalsetng.aspx?Type=User and to parse it in order to find the different regional settings.

If you're more interested about the Date format selected by the users, then I first search for the LCID (that I found using the regionalsetng.aspx page), and then I use the page "/_layouts/iframe.aspx?cal=1&date=1/1/2000&lcid="+lcid to find the date format. To do so, I parse the page and then:

// the document will contain the full datepicker page, for the January 2000
// search for 3/January/2000
var x = document.querySelector('a[id="20000103"]').getAttribute("href").replace(/javascript:ClickDay\('(.*)'\)/,"$1");
// source : http://stackoverflow.com/questions/7885096/how-do-i-decode-a-string-with-escaped-unicode
var r = /\\u([\d\w]{4})/gi;
x = x.replace(r, function (match, grp) { return String.fromCharCode(parseInt(grp, 16)); } );
x = unescape(x); // eg: 3.1.2000
x = x.replace(/20/, "YY"); // 3.1.YY00
x = x.replace(/00/, "YY"); // 3.1.YYYY
x = x.replace(/03/, "DD"); // 3.1.YYYY
x = x.replace(/3/, "D"); // D.1.YYYY
x = x.replace(/01/, "MM"); // D.1.YYYY
x = x.replace(/1/, "M"); // D.M.YYYY

So x will contain my format (YYYY for 4-digits Year, YY for 2-digits day, MM for 2-digits Month, M for 1-digit Month, DD for 2-digits day, D for 1-digit day).

For example x will return DD/MM/YYYY if you're French.

At the end I can use this format to translate a date, using something like the below (source), or MomentJS, or another Date library:

/**
 * Parse a string to a Date
 * @param  {String} strDate
 * @param  {String} format Supported format including YYYY, YY, MM, M, DD, D, with - and / separators
 * @return {Date|Throw} the JS Date object, or throw Error("Invalid Date") if strDate is invalid
 * @compatibility IE9+
 * @example
 * Date.parseFrom("1/10/2017", "MM/DD/YYYY"); // -> new Date(2017,0,10)
 */
Date.parseFrom=function(strDate, format) {
  var year,month,day;
  // find the separators
  var separator=" ";
  if (strDate.indexOf("-") > -1) separator="-";
  else if (strDate.indexOf("/") > -1) separator="/";
  strDate=strDate.split(separator);
  format=format.split(separator);
  format.forEach(function(d,i) {
    switch(d.charAt(0)) {
      case "Y": year=strDate[i]*1; break;
      case "M": month=strDate[i]*1-1; break;
      case "D": day=strDate[i]*1; break;
    }
  });
  if (year===undefined || month===undefined || day===undefined) throw Error('invalid date');
  return new Date(year,month,day);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.