最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - "Number Expected" Error in IE 11 with .toLocalString().toISOString() - Stack Overflow

programmeradmin2浏览0评论

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=".3.2/css/bootstrap.min.css" >
<script src=".11.2/jquery.min.js"></script>
<script src=".11.4/jquery-ui.js"></script>
<script src=".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>

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./bootstrap/3.3.2/css/bootstrap.min.css" >
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="https://code.jquery./ui/1.11.4/jquery-ui.js"></script>
<script src="https://maxcdn.bootstrapcdn./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>
Share Improve this question asked Aug 16, 2016 at 4:54 dikuwdikuw 1,16814 silver badges25 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 3

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

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).

发布评论

评论列表(0)

  1. 暂无评论