Moment: convertir números arábigos a números latinos, ¿cómo puedo hacer eso?

Creado en 27 dic. 2016  ·  3Comentarios  ·  Fuente: moment/moment

Solía ​​convertir números arábigos a números latinos haciendo lo siguiente

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

pero ahora, MomentLanguage no es un módulo en moment, ¿qué debo hacer?
PD: estoy usando la versión 2.15.2

Comentario más útil

@IbraheemAlSaady esto debería funcionar

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, '،');
      },
  });

Todos 3 comentarios

Su código está escrito con Typescript, ¿verdad? Creo que la interfaz que estás usando ahora se llama Locale .

Cerrando ya que no hemos tenido noticias desde el comentario de Lucas. Vuelva a abrir si aún necesita ayuda.

@IbraheemAlSaady esto debería funcionar

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, '،');
      },
  });
¿Fue útil esta página
0 / 5 - 0 calificaciones

Temas relacionados

ghost picture ghost  ·  3Comentarios

chitgoks picture chitgoks  ·  3Comentarios

RobinvanderVliet picture RobinvanderVliet  ·  3Comentarios

benhathaway picture benhathaway  ·  3Comentarios

danieljsinclair picture danieljsinclair  ·  3Comentarios