Solved

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

Posted on 2016-08-11
3
287 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…
Suggested Courses

617 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