Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
DATE IS COMING NaN-undefined-NaN NaN:NaN:NaN AM IN INTERNET EXPLORER BUT IN  GOOGLE CHROME IT IS COMING FINE


What I have tried:

var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

JavaScript
<pre>function formatDate(dt) {
    var current_datetime = new Date(dt);
    var hours = current_datetime.getHours() > 12 ? current_datetime.getHours() - 12 : current_datetime.getHours();
    var am_pm = current_datetime.getHours() >= 12 ? "PM" : "AM";
    hours = hours < 10 ? "0" + hours : hours;
    var minutes = current_datetime.getMinutes() < 10 ? "0" + current_datetime.getMinutes() : current_datetime.getMinutes();
    var seconds = current_datetime.getSeconds() < 10 ? "0" + current_datetime.getSeconds() : current_datetime.getSeconds();
    time = hours + ":" + minutes + ":" + seconds + " " + am_pm;
    var formatted_date = current_datetime.getDate() + "-" + months[current_datetime.getMonth()] + "-" + current_datetime.getFullYear() + ' ' + time;
    return formatted_date;
    
}


self.LastSaveDate(formatDate(new Date(item.LastSaveDate + ' UTC').toString()));
Posted
Updated 12-Oct-20 20:13pm
Comments
sweta dash 13-Oct-20 3:41am    
It is working fine in Chorome but not in internet Explorer
Richard Deeming 13-Oct-20 7:41am    
It's not clear why you're converting a string to a date, back to a string, back to a date, and finally back to a string.

The error is most likely due to Internet Explorer not supporting the date format you're using. But without seeing the value you're passing in to the function, we can't tell you exactly what the problem is.
Richard Deeming 13-Oct-20 7:42am    
Oh, and even Microsoft will tell you that Internet Explorer is not a browser:
Microsoft security chief: IE is not a browser, so stop using it as your default | ZDNet[^]

1 solution

It works fine in Chrome for me:
HTML
<!DOCTYPE html>
<html>
    <body onload="myFunction()">
        <h1>Hello World!</h1>
        <script>
            function myFunction() {
              document.getElementById('demo').innerHTML = Date();
              document.getElementById('demo2').innerHTML = formatDate(Date());
            }
            var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
            function formatDate(dt) {
                var current_datetime = new Date(dt);
                var hours = current_datetime.getHours() > 12 ? current_datetime.getHours() - 12 : current_datetime.getHours();
                var am_pm = current_datetime.getHours() >= 12 ? "PM" : "AM";
                hours = hours < 10 ? "0" + hours : hours;
                var minutes = current_datetime.getMinutes() < 10 ? "0" + current_datetime.getMinutes() : current_datetime.getMinutes();
                var seconds = current_datetime.getSeconds() < 10 ? "0" + current_datetime.getSeconds() : current_datetime.getSeconds();
                time = hours + ":" + minutes + ":" + seconds + " " + am_pm;
                var formatted_date = current_datetime.getDate() + "-" + months[current_datetime.getMonth()] + "-" + current_datetime.getFullYear() + ' ' + time;
                return formatted_date;
            }
        </script>
        <p id="demo">Default</p>
        <p id="demo2">Default</p>
    </body>
</html>

I get this:
Hello World!
Tue Oct 13 2020 07:10:56 GMT+0100 (British Summer Time)

13-Oct-2020 07:10:56 AM
So what am I doing that is different to you?
 
Share this answer
 
Comments
sweta dash 13-Oct-20 3:42am    
It is working fine in chrome but not in internet explorer that is the issue
OriginalGriff 13-Oct-20 4:12am    
Does the same in IE 11 on Win10 for me:

Hello World!
 
Tue Oct 13 2020 09:11:29 GMT+0100 (GMT Daylight Time)

13-Oct-2020 09:11:29 AM
sweta dash 14-Oct-20 5:35am    
Actually I am binding to a dropdown converting into in IST time at that time I am getting this error.self.LastSaveDate(formatDate(new Date(item.LastSaveDate + ' UTC').toString()));

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900