0

I am using below function. It is giving the output as Jul-7,-1988 for few users. It is working fine for all the users and giving correct result like 7-Jul-1988. Anyone knows how to fix this issue. I want the result to be in 07-Jul-1988 format for all the users. And why does it is changing into different format when I have specifically mention the format 'en-GB'.

function(data) {
    var date = new Date(data);  
   
    return date.toLocaleDateString('en-GB', {
        day: 'numeric', month: 'short', year: 'numeric'
      }).replace(/ /g, '-')
}

I have found on this link - https://github.com/nodejs/node/issues/33761 that it behave differently. In my case also it could be the same issue ?

Update: Can I use the below approach to get the date in desired format or moment.js would be better idea?

    function(data){
    var months = ["JAN", "FEB", "MAR","APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"];
   var current_datetime = new Date(data);
    var formatted_date = current_datetime.getDate() + "-" + months[current_datetime.getMonth()] + "-" + current_datetime.getFullYear()
   console.log(formatted_date)
   }
rajat
  • 29
  • 3
  • Users have control over how exactly they want localized dates to display. Even if my system is set to `en-GB` locale, I can still _specify_ what exactly I want that to look like, and/or what order I want the date components to display in. – CBroe Jan 26 '21 at 14:38
  • locale is not definied by standards (the names yes, and the format, but not the content). Every OS and language could choose different locale definitions. – Giacomo Catenazzi Jan 26 '21 at 14:51
  • @CBroe so what can I try to fix the issue ? – rajat Jan 26 '21 at 15:14
  • The output of `toLocaleDateString` is meant to be a human-readable string with as much of what you specify applied as makes sense in the given format/locale. It's **definitely not** a fixed format that you should expect and try to tweak further. If you want a fixed format, don't use methods meant for locale-specific formatting. – Joachim Sauer Jan 26 '21 at 15:33
  • @JoachimSauer Can I use this function(data){ var months = ["JAN", "FEB", "MAR","APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"]; var current_datetime = new Date(data); var formatted_date = current_datetime.getDate() + "-" + months[current_datetime.getMonth()] + "-" + current_datetime.getFullYear() console.log(formatted_date) } – rajat Jan 26 '21 at 16:56
  • @CBroe—re "*Users have control over how exactly they want localized dates to display*" No they don't, the format is entirely up to the host developers. The rest of what you say is correct though: ECMA-262 suggests using the [*CLDR*](http://cldr.unicode.org), but it's not required and even then there are variations. – RobG Jan 26 '21 at 21:54

0 Answers0