Moment: convert Arabic numbers to Latin numbers, how can I do that?

Created on 27 Dec 2016  ·  3Comments  ·  Source: moment/moment

I used to convert Arabic numbers to Latin numbers by doing the following

      moment.updateLocale('ar', <MomentLanguage>{
        preparse: (str) => {
            return str.replace(/\u200f/g, '');
        },
        postformat: (str) => {
            return str;
        }
      });

but now, MomentLanguage is not a module in moment, what should I do?
PS: I'm using version 2.15.2

Most helpful comment

@IbraheemAlSaady this should work

const symbolMap = {
        '1': '1',
        '2': '2',
        '3': '3',
        '4': '4',
        '5': '5',
        '6': '6',
        '7': '7',
        '8': '8',
        '9': '9',
        '0': '0'
    };
    const numberMap = {
        '١': '1',
        '٢': '2',
        '٣': '3',
        '٤': '4',
        '٥': '5',
        '٦': '6',
        '٧': '7',
        '٨': '8',
        '٩': '9',
        '٠': '0'
    }
  moment.updateLocale('ar', {
      preparse: function (string) {
          return string.replace(/\u200f/g, '').replace(/[١٢٣٤٥٦٧٨٩٠]/g, function (match) {
              return numberMap[match];
          }).replace(/،/g, ',');
      },
      postformat: function(string) {
        return string.replace(/\d/g, function(match) {
          return symbolMap[match];
        }).replace(/,/g, '،');
      },
  });

All 3 comments

Your code is written with Typescript, right? I think the interface you're using is called Locale now.

Closing since we haven't heard back since Lucas' comment. Reopen if you still need help.

@IbraheemAlSaady this should work

const symbolMap = {
        '1': '1',
        '2': '2',
        '3': '3',
        '4': '4',
        '5': '5',
        '6': '6',
        '7': '7',
        '8': '8',
        '9': '9',
        '0': '0'
    };
    const numberMap = {
        '١': '1',
        '٢': '2',
        '٣': '3',
        '٤': '4',
        '٥': '5',
        '٦': '6',
        '٧': '7',
        '٨': '8',
        '٩': '9',
        '٠': '0'
    }
  moment.updateLocale('ar', {
      preparse: function (string) {
          return string.replace(/\u200f/g, '').replace(/[١٢٣٤٥٦٧٨٩٠]/g, function (match) {
              return numberMap[match];
          }).replace(/،/g, ',');
      },
      postformat: function(string) {
        return string.replace(/\d/g, function(match) {
          return symbolMap[match];
        }).replace(/,/g, '،');
      },
  });
Was this page helpful?
0 / 5 - 0 ratings