Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Number Expected" Error in IE 11 with .toLocalString()/.toISOString()

I'm getting the error "Number expected" at the line:

new Date($('#itemOpenDate').val()).toISOString();

where $('#itemOpenDate').val() is a string representing a date, e.g. "8/11/2016", in IE 11 only (tests fine in Chrome, Firefox, and Safari).

I've narrowed it down to how #itemOpenDate is populated. See the below code. I get the error with:

('#itemOpenDate').val(new Date("8/2/2016").toLocaleString());

but the following works fine (even in IE 11):

$('#itemOpenDate').val(new Date("8/2/2016").toDateString());

Of course, I want the format .toLocaleString provides. Any ideas?

Code to reproduce:

<!doctype html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" >
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container" role="main">
    <form class="center-block form-horizontal">
        <div class="form-group">
            <label for="itemOpenDate" class="control-label">Open Date:</label>
            <input type="text" class="form-control input-lg" id="itemOpenDate">
        </div>

        <div class="form-group">
            <button id="updateItemButton"  title="Update Item" type="button" class="btn btn-primary btn-lg btn-block">Update</button>
        </div>
    </form>
</div>

<script type="text/javascript">

$(document).ready(function () {

$("#itemOpenDate").datepicker();

getPunchlistItem();

$("#updateItemButton").click(function () {
    updatePunchlistItem();
});
});

function getPunchlistItem(myPLItemID) {
$('#itemOpenDate').val(new Date("8/2/2016").toLocaleString());  //  error
$('#itemOpenDate').val(new Date("8/2/2016").toDateString());  //  no error
}

function updatePunchlistItem() {
var myOpenDate = new Date($('#itemOpenDate').val()).toISOString();
console.log(myOpenDate);
}
</script>
</body>
</html>
like image 382
dikuw Avatar asked Aug 16 '16 04:08

dikuw


2 Answers

I have also seen this issue but in a different way.

var date = new Date('2017-07-02 00:00:00Z').toISOString();

this works in chrome but not IE11. Adding a T between the date and the time resolves the issue in IE11 and it still works in Chrome.

var date = new Date('2017-07-02T00:00:00Z').toISOString();

http://plnkr.co/edit/8WAhslAphwR3BHxnq3bE?p=preview

like image 57
Paul Avatar answered Nov 10 '22 22:11

Paul


where $('#itemOpenDate').val() is a string representing a date, e.g. "8/11/2016"

There's your problem. JavaScript's Date constructor is not required to support anything but the date/time format in the specification, which is (as of the ES2015 specifiation) a subset of ISO-8601 (in the ES5 spec, they'd made a mistake that made it not quite a subset), e.g. 2016-08-11. Any time you ask it to parse anything else, you're wandering into undefined behavior. You'll need to parse the string on purpose (with your own code, or with a library like MomentJS).

like image 2
T.J. Crowder Avatar answered Nov 10 '22 21:11

T.J. Crowder