Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
I am getting the date format different in IE 8 & IE 11 Browser

In IE 8 i am getting the date as
C#
Tuesday , November 24,2015 2:00:47 PM



while In IE 11 i am getting the date as
11/24/2015 2:00:47 PM


I want the same date format as in IE 8.
my code is as below:

HTML
var dt = new Date();

document.getElementById("<%= TextBox1.ClientID %>").value =  dt.toLocaleDateString()+' '+ dt.toLocaleTimeString();
Posted
Updated 23-Nov-15 23:17pm
v2
Comments
F-ES Sitecore 24-Nov-15 5:21am    
If you want it in a specific format then build the date string yourself. Google "javascript date format" for example code.
Afzaal Ahmad Zeeshan 24-Nov-15 7:03am    
I would also recommend that you override the date time format to provide a similar string in every device and browser.

Few days back, I ran into the same problem and after GOOGLING found a solution as per my requirements.

Hope it will help to fulfill the OP's need or atleast will give an idea to fulfill them.

Add the below methods for date formatting,

JavaScript
function makeArray() {
    for (i = 0; i < makeArray.arguments.length; i++)
        this[i] = makeArray.arguments[i];
}

function getFullYear(d) {
    var y = d.getYear();
    if (y < 1000) { y += 1900 };
    return y;
}

var days = new makeArray("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
var months = new makeArray("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

function formatDate(t) {
    var Day = t.getDay();
    var Date = t.getDate();
    var Month = t.getMonth();
    var Year = getFullYear(t);
    timeString = "";
    timeString += days[Day];
    timeString += ", ";
    timeString += months[Month];
    timeString += " ";
    timeString += Date;
    timeString += ", ";
    timeString += Year;
    return timeString;
}



and than simply call

HTML
formatDate(new Date())


it will return Tuesday, November 24, 2015.
 
Share this answer
 
v2
Try this :

JavaScript
var d = new Date();

           var weekday = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
           var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']

           var hours = d.getHours();
           hours = (hours + 24) % 24;
           var Mid ='AM';
           if(hours == 0){
               hours = 12;
           }
           else if(hours > 12)
           {
                hours = hours % 12;
                Mid='PM';
           }

           var datestring = weekday[d.getDay()] + ', ' + months[d.getMonth()] + ' ' + d.getDate() + ", " + d.getFullYear() + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getMilliseconds() + " " + Mid;

          document.getElementById("<%= TextBox1.ClientID %>").value = datestring;

[Edit] : Removed -2

Good luck.
 
Share this answer
 
v2
Comments
Philippe Mori 24-Nov-15 12:22pm    
What is the purpose of -2? It does not make any sense...
Raje_ 25-Nov-15 4:58am    
You are right. It does not make any sense. I have removed that. Thank you for pointing out this mistake. :)

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