Solved

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

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

773 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