logan
logan

Reputation: 8346

Javascript running Date Time return value is working in chrome but not in Internet explorer

I have a javascript function to run time in different manner. Its working well in Chrome browser but its not showing proper value in Internet Explorer.

In Chrome:

13-Dec-2011 13:14:19

In IE8:

0NaN-undefined-NaN 0NaN:0NaN:0NaN

You can view this from this page link from both the browsers. Also look at the source code of page http://chemfluence.org.in/monetarist/sample.php

My Code:

 <div id="txt" title="Industries will generate products on every 3 Virtual days. 
 12 Virtual Days = 1 Real day. dd-Mon-yyyy HH:MM:SS ."></div>

Javascript

<script type='text/javascript'>//<![CDATA[ 

var virtualOrigin = Date.parse("2012-02-27T00:00:00"),
    game_start_realdate=Date.parse("2013-01-27T12:00:00"),
    realOrigin = Date.now(),
    factor = 12;

function getVirtual(time) {
    return new Date( virtualOrigin + ((time - realOrigin) * factor)  +(realOrigin-game_start_realdate)*factor);
}

function pad2(num) {
    return ("0"+num).substr(-2);
}
function format(time) {
var month=new Array();
month[0]="Jan";
month[1]="Feb";
month[2]="Mar";
month[3]="Apr";
month[4]="May";
month[5]="Jun";
month[6]="Jul";
month[7]="Aug";
month[8]="Sep";
month[9]="Oct";
month[10]="Nov";
month[11]="Dec";
     return pad2(time.getDate())
        + "-" + month[time.getMonth()]
        + "-" + time.getFullYear()
        + " " + pad2(time.getHours())
        + ":" + pad2(time.getMinutes())
        + ":" + pad2(time.getSeconds());
}

function startTime() {
    var now = new Date();
    var display = getVirtual(now);
    output.innerText = format(display);
    setTimeout(startTime, 1000/factor - (now.getMilliseconds() % (1000/factor)));
}

var output = document.getElementById("txt");
startTime();

</script>

I need the above Javascript to be modified to work in Internet Explore and Chrome/Firefox; Please give me modified code;

Upvotes: 2

Views: 1977

Answers (2)

aaaaaaaaaaaa
aaaaaaaaaaaa

Reputation: 3700

Take a custom parse function to be sure that you can parse that date format whatever the browser is:

function parse(datestring){
    var timearray = datestring.split(/[\-T\:]/g)
    return +new Date(+timearray[0],timearray[1]-1,+timearray[2],+timearray[3],+timearray[4],+timearray[5])
}

And fix the pad2 function to work with IE8 by not using substr with a negative value:

function pad2(num) {
    return ("0"+num).slice(-2);
}

That should do it.

Upvotes: 2

brenjt
brenjt

Reputation: 16297

Try using moment.js. it is cross browser and can make doing dates in javascript much less of a pain. The documentation is very thorough. http://momentjs.com/docs/

You can format your date with as little as:

var day = moment("12-25-1995", "MM-DD-YYYY");

http://momentjs.com/docs/#/parsing/string-format/

UPDATE

Here is an example of it's full usage.

var mysql_date = '2013-01-25 10:00:00'; // Date from MySQL database

/**
 * @param     string     mysql_data     Date string
 * @param     string     format         Format in which mysql_data is set
 */
var date = moment(mysql_date , 'YYYY-MM-DD HH:mm:ss'); // new moment.js object.

// To display the date in a different format use:
var date_format1 = date.format('MMM, Do'); // Format here would be Jan, 25th
var date_format2 = date.format('MMMM, Do, YYYY'); // January, 25th, 2013

console.log(date_format1, date_format2);

You can change the format when ever you want. You don't need to recreate it again.

Upvotes: 3

Related Questions