0

I used the link below for convert Jalali to Gregorian:

https://github.com/Mds92/MD.BootstrapPersianDateTimePicker/tree/master/MD.BootstrapPersianDateTimePicker/Scripts

I receive data from user as string. And this is the code I use:

<script>
    var jj = document.getElementById("fromDate1"),
            bb = document.getElementById("showMe"),
            splitOb, yy, mm, dd;

    bb.onclick = function () {
        splitOb = jj.value.split("/");
        for (var i = 0; i < splitOb.length; i++) {
            yy = splitOb[0];
            mm = splitOb[1];
            dd = splitOb[2];
        }

        var xx = yy.trim().toString(), nn = mm.trim().toString(), mmm = dd.trim().toString();

        var xxx = parseInt(xx, 10);
        var nnn = parseInt(nn, 10);
        var mjj = parseInt(mmm, 10);
        var hello = toGregorian(xxx, nnn, mjj);
        alert(hello.gy + "/" + hello.gm + "/" + hello.gd);

        /* var gh= "1395";
         var ghh = parseInt(gh);
         alert(ghh);*/
    };
</script>

I used parseInt in my code and unfortunately the result is Nan, I checked my variables, all of them was strings. But when I convert them from string to integer the result is NaN too. when I set string to my variables manually like this code:

var jjj = "1395";
        var yyyt = "05";
        var kik = "04";

        var xxx = parseInt(jjj, 10);
        var nnn = parseInt(yyyt, 10);
        var mjj = parseInt(kik, 10);

        var hello = toGregorian(xxx, nnn, mjj);
        alert(hello.gy + "/" + hello.gm + "/" + hello.gd);

Everything works fine, why?

styopdev
  • 2,604
  • 4
  • 34
  • 55
  • what happens if you do a ```console.log``` on xx, nn and mmm? – Philipp Jul 25 '16 at 08:21
  • I just checked, this code is working. I dont know where do you have problem. try alert(jjj + ' ' + yyyt + ' ' + kik); and you will get number results... not NaN – Aleksandar Đokić Jul 25 '16 at 08:22
  • The proper tool to inspect variables is the [console](https://getfirebug.com/wiki/index.php/Console_API). Good old [alert()](https://developer.mozilla.org/en-US/docs/Web/API/Window/alert) casts everything to string thus hides relevant information. It's impossible to say what's wrong with your code since we don't really know the content of any variable. – Álvaro González Jul 25 '16 at 08:25

3 Answers3

1

NaN means Not A Number. Maybe you could eliminate that toString() part.

<script>
    var jj = document.getElementById("fromDate1"),
            bb = document.getElementById("showMe"),
            splitOb, yy, mm, dd;

    bb.onclick = function () {
        splitOb = jj.value.split("/");
        for (var i = 0; i < splitOb.length; i++) {
            yy = splitOb[0];
            mm = splitOb[1];
            dd = splitOb[2];
        }

        var xxx = parseInt(yy, 10);
        var nnn = parseInt(mm, 10);
        var mjj = parseInt(dd, 10);
        var hello = toGregorian(xxx, nnn, mjj);
        alert(hello.gy + "/" + hello.gm + "/" + hello.gd);

        /* var gh= "1395";
         var ghh = parseInt(gh);
         alert(ghh);*/
    };
</script>
0

This could help answer your question. It seems that it's unable to convert the first character or some of the characters to a numerical value. That's what is causing the issue.

Community
  • 1
  • 1
Arucious
  • 1
  • 1
0

I GOT IT !! The problem was that the string that I get form users was Persian/Arabic. I should change it to English string numbers. I used this code to solve the problem:

function parseArabic(str) {
        return Number( str.replace(/[٠١٢٣٤٥٦٧٨٩]/g, function(d) {
            return d.charCodeAt(0) - 1632;
        }).replace(/[۰۱۲۳۴۵۶۷۸۹]/g, function(d) {
            return d.charCodeAt(0) - 1776;
        }) );
    }

I would be appreciate if you have another customized code to tell me. Thanks for your consideration.