0

How can I convert a month and year value into Unicode using Javascript? For instance, "6, 2013" would become "\u1046, \u1042\u1040\u1041\u1043" (Myanmar text).

Patrick pointed me to Codepen where I got this:

 <script type="text/javascript">
  var d = new Date();
  var mn = (d.getMonth()+1);
  var dn = d.getDate();
  var yn = d.getFullYear();

  var toMyanmar = function (string) {
    var unicodeString = '';
    for (var i=0; i < string.length; i++) {
      var char = string[i];
      if (parseInt(char),16) {
        char = String.fromCharCode(string.charCodeAt(i) + 4112)
      }
      unicodeString += char;
    }
    return unicodeString;
  }

  document.write("<br/>Myanmar Date: ");
  document.write(toMyanmar(''+ mn) + ', ');
  document.write(toMyanmar(''+ dn) + ', ');
  document.write(toMyanmar(''+ yn));
  document.write("<br/>Date; Latin/Arabic numerals: " + mn + ', ' + dn + ', ' + yn);
</script>

<body style="font-family:'Myanmar Text',Arial;"></body>

Zero (0; u1040, #4160) was ignored by this script until I moved the radix number to the same line as "parseInt". (mmrtext.ttf or similar Unicode font is needed to see characters displayed.) So, this now works well for my purpose. And I think will be be the basis for future transcoding of character pages since Myanmar-related languages use many different sets. Thanks a million until you are better paid. R. Holland

  • There is a framework named something like `Globalize` that can do localization of dates. – Lee Meador Jun 03 '13 at 19:10
  • All characters you use are Unicode. There is no “extended unicode”. Please specify your problems in terms of localization, as this appears to be the issue. How would you like to localize a value, and which value? (“Date” and “month and year” are different issues.) Also please explain why you would do localization in JavaScript, rather than when generating a page. – Jukka K. Korpela Jun 03 '13 at 19:15

1 Answers1

0

Check out http://buildingonmud.blogspot.com/2009/06/convert-string-to-unicode-in-javascript.html

You will have to add some checking if you only want to encode numbers (run each element through parseInt most likely), since it would encode the command and the spaces as well.

Patrick
  • 13,872
  • 5
  • 35
  • 53
  • I want to output the year, returned as 2013, as "\u1042\u1040\u1041\u1043". Thank you. – richolland Jun 05 '13 at 00:19
  • check out http://codepen.io/patrickkettner/pen/0db3c0fcf4f66430cf9f11659a294613 , all they do are digits to mayanmar digits. – Patrick Jun 05 '13 at 01:37
  • Codepen had a major step in the right direction. Thank you, Patrick. Richard H. – richolland Jun 06 '13 at 19:12
  • For my purposes, this has been answered/solved. It was by far the most difficult question to get a solution for. Good job, Patrick. – richolland Jun 07 '13 at 14:22
  • Great, glad to have helped. You should mark a question as answered if you feel it answered the original - or if none of them really did, post your own answer. – Patrick Jun 07 '13 at 17:47