Solved

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

Posted on 2016-08-11
3
121 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
JavaScript waiting 14 69
Polygon I google maps 1 16
javascript date picker in php while loop 5 27
how to avoid redundand ajax calls 5 9
Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn how to dynamically set the form action using jQuery.
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…

920 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

16 Experts available now in Live!

Get 1:1 Help Now