JavaScript Type Conversion Not A Number

clockwiseq

I have a div with a data attribute that contains a date. When I retrieve said data value, it's not a date, but a string:

<div id="firstRow" class="form-group" data-expense-date="5/3/2016">
    <span>some stuff in here</span>
</div>

I'm retrieving the data element like so:

var currentDateString = $(this).data('expense-date');

I then am attempting to split the value into an array of strings:

var currentDateArray = currentDateString.split("/");

I then assign parts of the array as variables of my date I am trying to create:

var year = Number(currentDateArray[2].toString());
var month = Number(currentDateArray[0].toString());
var day = Number(currentDateArray[1].toString());

Interestingly enough, all three of my variables say that they are NaN. Thus, the declaration of my new Date variable is "Invalid Date":

var currentDate = new Date(year, month - 1, day);

I can do the following and it works just fine:

var dateString = "5/3/2016";
var dateStringArray = dateString.split("/");
var testDate = new Date(dateStringArray[2], dateStringArray[0] - 1, dateStringArray[1]);

It's only when I read the data attribute from the div element that sets the value to something that I do not know what it is.

I am debugging in Visual Studio and this is a screenshot of my Immediate Window trying to test the values:

enter image description here

Here is the complete example...my HTML:

<div class="col-md-6" id="detailPreview">
    <div class="panel panel-transparent" data-expense-date="5/2/2016">                             
        <div class="panel-heading">                                 
            <h3 class="panel-title">Monday, May 2, 2016</h3>                                 
            <div class="panel-actions">                                     
                <a class="panel-action icon wb-minus" data-toggle="panel-collapse" aria-expanded="true" aria-hidden="true"></a>                                 
            </div>                             
        </div>                             
        <div class="panel-body expense-date-body">                            
            <div class="panel" data-expense-detail-id="5e18bb2b-4671-490d-8769-5d3ea08134d8">                                 
                <div class="panel-heading">                                     
                    <h3 class="panel-title"><i class="icon wb-payment" aria-hidden="true"></i>Expense: Best Buy (ASUS Z555RA-3)</h3>                                    
                    <div class="panel-actions">                                         
                        <a class="panel-action panel-action-edit icon wb-settings" aria-hidden="true"></a>                                         
                        <a class="panel-action icon wb-minus" data-toggle="panel-collapse" aria-expanded="false" aria-hidden="true"></a>                                         
                        <a class="panel-action icon wb-close" data-toggle="panel-close" aria-hidden="true"></a>                                     
                    </div>                                 
                </div>                                 
                <!-- <div class="panel-collapse"> -->                                     
                <div class="panel-body">                                    
                    Business Name: Best Buy                                         
                    Location: Topeka, KS                                         
                    Purpose: Purchased replacement laptop for inventory                                     
                </div>                                 
                <!-- </div> -->                              
            </div>
        </div>                          
    </div>
    <div class="panel panel-transparent" data-expense-date="5/3/2016">                             
        <div class="panel-heading">                                 
            <h3 class="panel-title">Tuesday, May 3, 2016</h3>                                 
            <div class="panel-actions">                                     
                <a class="panel-action icon wb-minus" data-toggle="panel-collapse" aria-expanded="true" aria-hidden="true"></a>                                 
            </div>                             
        </div>                             
        <div class="panel-body expense-date-body">                             
            <div class="panel" data-expense-detail-id="49de85e2-b3ef-465d-8a61-cbc298b99d34">                                 
                <div class="panel-heading">                                     
                    <h3 class="panel-title"><i class="icon wb-payment" aria-hidden="true"></i>Expense: Wal-Mart (Scotch Tape)</h3>                                    
                    <div class="panel-actions">                                         
                        <a class="panel-action panel-action-edit icon wb-settings" aria-hidden="true"></a>                                         
                        <a class="panel-action icon wb-minus" data-toggle="panel-collapse" aria-expanded="false" aria-hidden="true"></a>                                         
                        <a class="panel-action icon wb-close" data-toggle="panel-close" aria-hidden="true"></a>                                     
                    </div>                                 
                </div>                                 
                <!-- <div class="panel-collapse"> -->                                     
                <div class="panel-body">                                         
                    Business Name: Wal-Mart                                         
                    Location: Topeka, KS                                         
                    Purpose: Purchased scotch tape                                     
                </div>                                 
                <!-- </div> -->                              
            </div>
        </div>                          
    </div>
</div>

My javascript:

$("[data-expense-date]").each(function () {
  if ($(this).data('expense-date') === dateShort.toString()) {
    parentDiv = $(this);
    return false;
  } else {
    debugger;

    // determine if this date is before or after the param date.
    //var dataExpenseDate = $(this).data('expense-date');
    var currentDateString = $(this).data('expense-date'); // "5/2/2016"

    var currentDateArray = currentDateString.split('/'); // [5,2,2016]
    //var yearString = currentDateArray[2];
    //var yearNumber = Number(yearString);
    var year = Number(currentDateArray[2]); // "2016" <-- NaN
    var month = Number(currentDateArray[0]); // "5" <-- NaN
    var day = Number(currentDateArray[1]); // "3" <-- NaN

    var currentDate = new Date(year, month - 1, day); // Invalid Date

    if (date > currentDate) {
        newDivInjectAfter = $(this);
    }
  }
});

UPDATE:
This only happens when I am using Internet Explorer (11). Edge, Opera, FireFox, and Chrome, ALL parse the values properly. Thanks for all of the responses guys, but I believe I have discovered a bug with IE.

clockwiseq

After searching for my issue and appending Internet Explorer to the search, I get a hit here. And it appears to have been resolved using RegEx:

Strange behaviour with spliting a string in Javascript

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Is possible avoid internal number conversion on isNaN() function in JavaScript?

From Dev

Huge binary number Conversion

From Dev

Currency to number conversion rails

From Dev

Validating String and Type Conversion in JavaScript

From Dev

Type aware string to number conversion in C++

From Dev

JS type conversion between string and number

From Dev

Type conversion adding and subtracting an number index value in an array

From Dev

Detecting a number overflow in JavaScript at the time of base10 string conversion

From Dev

Javascript Number-String DIV onclick strange conversion

From Dev

Javascript variable declaration and number conversion

From Dev

string to number conversion in golang

From Dev

What is difference between type=number and type=text in JavaScript regex?

From Dev

Number Format in Javascript + conversion?

From Dev

How does type conversion happens in JavaScript?

From Dev

String conversion to Number

From Dev

Type conversion

From Dev

Large number base conversion

From Dev

Currency to number conversion rails

From Dev

Validating String and Type Conversion in JavaScript

From Dev

Type aware string to number conversion in C++

From Dev

why is javascript turning my date into a type of number

From Dev

string to number conversion in prolog

From Dev

ELK Type Conversion - Not a number but a string

From Dev

Javascript Comparison Operators/Number Conversion Not Working

From Dev

conversion string type into integer type in tcl script. like that a number is type of string and I want to increment it so how to fix it in tcl

From Dev

Number Conversion in SSRS

From Dev

PL/LUA type ERROR: [pllua]: raw datum expected for datum conversion, got number

From Dev

Javascript data type for storing large number of elements

From Dev

How to add a number to a variable in JavaScript for temperature conversion?

Related Related

  1. 1

    Is possible avoid internal number conversion on isNaN() function in JavaScript?

  2. 2

    Huge binary number Conversion

  3. 3

    Currency to number conversion rails

  4. 4

    Validating String and Type Conversion in JavaScript

  5. 5

    Type aware string to number conversion in C++

  6. 6

    JS type conversion between string and number

  7. 7

    Type conversion adding and subtracting an number index value in an array

  8. 8

    Detecting a number overflow in JavaScript at the time of base10 string conversion

  9. 9

    Javascript Number-String DIV onclick strange conversion

  10. 10

    Javascript variable declaration and number conversion

  11. 11

    string to number conversion in golang

  12. 12

    What is difference between type=number and type=text in JavaScript regex?

  13. 13

    Number Format in Javascript + conversion?

  14. 14

    How does type conversion happens in JavaScript?

  15. 15

    String conversion to Number

  16. 16

    Type conversion

  17. 17

    Large number base conversion

  18. 18

    Currency to number conversion rails

  19. 19

    Validating String and Type Conversion in JavaScript

  20. 20

    Type aware string to number conversion in C++

  21. 21

    why is javascript turning my date into a type of number

  22. 22

    string to number conversion in prolog

  23. 23

    ELK Type Conversion - Not a number but a string

  24. 24

    Javascript Comparison Operators/Number Conversion Not Working

  25. 25

    conversion string type into integer type in tcl script. like that a number is type of string and I want to increment it so how to fix it in tcl

  26. 26

    Number Conversion in SSRS

  27. 27

    PL/LUA type ERROR: [pllua]: raw datum expected for datum conversion, got number

  28. 28

    Javascript data type for storing large number of elements

  29. 29

    How to add a number to a variable in JavaScript for temperature conversion?

HotTag

Archive