javascript - Format region-specific dates to dd-MMM-yyyy? -


we have local sharepoint install in vendor company had written javascript function parse date fields in given list , display them in d-mmm-yyyy (e.g. 24-dec-2015) on page load. however, date parsing fail when user changed regional settings united states (english) united kingdom (english), date format changed mm/dd/yyyy dd/mm/yyyy. likewise other locales.

keeping javascript solution, best way parse date based on dynamic locale?

currently, i'm able fetch user's selected lcid , retrieve culture code (e.g. en-us) , pass sugarjs library so:

var currentculture = "";  function configureculture(){     //to accurately retrieve user culture, scrape regional settings page lcid     //this terrible hack, couldn't find other solution     var regionalsettingsurl = ctx.httproot+"/_layouts/regionalsetng.aspx?type=user";      jquery.ajax({          url: regionalsettingsurl,          async: false,         datatype: 'html',         success: function(data) {             var lcid = jquery(data).find("select[name$='lcid'] option:selected")[0].value;             currentculture = lcidutils.getculturename(lcid);             },         error: function(jqxhr, textstatus, errorthrown){               //debugmessage(errorthrown);             }     }); }  //passeddate param string representation of date //actual date varies based on culture, must parse appropriately //e.g. 12/3/2014 3-dec-2014 in united states  //but 12-mar-2014 in united kingdom function applydateformat(passeddate) {     if(currentculture == ""){         configureculture();     }      //uses sugarjs libary create date based on current culture code     return date.create(passeddate, currentculture).format('d-mmm-yyyy'); } 

are there better solutions? note displaying list in standard view, , underlying data not affected.

have @ tolocalestring / tolocaleformat methods of date object. maybe they're useful here.

https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/date/tolocalestring


Comments