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:
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.
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:
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments