Solved

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

Posted on 2016-08-11
3
95 Views
Last Modified: 2016-08-17
I'm getting the error "Number expected" at the line:

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

Open in new window



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

No problems in the latest version of Chrome, Firefox, and Safari. Should be supported in IE11 per the documentation. Am I missing something?
0
Comment
Question by:Michael Vasilevsky
  • 2
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 41756131
go over, don't fight with IE, use this :
var arr = $('#itemOpenDate').val().split("/");
new Date(arr[2], arr[0]*1-1, arr[1]).toISOString();

Open in new window

0
 
LVL 10

Author Comment

by:Michael Vasilevsky
ID: 41757359
That should work, but IE 11 gives me NaN for arr[0] * 1 - 1. Other browsers return the expected...

I've narrowed it down to how I'm populating the form field. See the below code. For line 43, the following works in IE11:

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

Open in new window


but I want the locale string format "8/2/2016", and the following breaks (in IE11 only):

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

Open in new window


Any ideas?

<!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());
}

function updatePunchlistItem() {

    var myOpenDate = new Date($('#itemOpenDate').val()).toISOString();
    console.log(myOpenDate);

}


</script>

</body>

</html>

Open in new window

0
 
LVL 10

Author Comment

by:Michael Vasilevsky
ID: 41759018
I got it to work by using momentjs in line 43, instead of
toLocaleString()

Open in new window

:

$('#itemOpenDate').val(moment("8/2/2016").format("L"));

Open in new window

0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now