I am trying to display the date on android emulator browser. The current code works fine on my PC web browser but it does not work properly on Android emulator web browser. Here are my two functions which I am using for this purpose.
dayOfWeek('Sun Jan 08 2012 02:00:00 GMT+0200 (FLE Standard Time)');
dayOfWeek = function (d) {
var dayNames = new Array('SUN','MON','TUE','WED','THU','FRI','SAT');
var day = toTimestamp(d);
//alert(day);
return dayNames[day.getDay()];
}
toTimestamp = function (strDate){
return new Date(Date.parse(strDate));
}
I am passing date in this format “Sun Jan 08 2012 02:00:00 GMT+0200 (FLE Standard Time)” which you can see in the code above as well.
PROBLEM:
The problem is on Android emulator browser it displays “undefined” where ti should appear name of the day such as “MON, TUE, WED” etc.
How can I solve this problem?
In the past I had the same problem then it was solved with following function. I got this function from somewhere on internet. But now it does not work here I do not know why.
function relative_time(date_str) {
if (!date_str) {return;}
date_str = $.trim(date_str);
date_str = date_str.replace(/\.\d\d\d+/,""); // remove the milliseconds
date_str = date_str.replace(/-/,"/").replace(/-/,"/"); //substitute - with /
date_str = date_str.replace(/T/," ").replace(/Z/," UTC"); //remove T and substitute Z with UTC
date_str = date_str.replace(/([\+\-]\d\d)\:?(\d\d)/," $1$2"); // +08:00 -> +0800
var parsed_date = new Date(date_str);
var relative_to = (arguments.length > 1) ? arguments[1] : new Date(); //defines relative to what ..default is now
var delta = parseInt((relative_to.getTime()-parsed_date)/1000);
delta=(delta<2)?2:delta;
var r = '';
if (delta < 60) {
r = delta + ' seconds ago';
} else if(delta < 120) {
r = 'a minute ago';
} else if(delta < (45*60)) {
r = (parseInt(delta / 60, 10)).toString() + ' minutes ago';
} else if(delta < (2*60*60)) {
r = 'an hour ago';
} else if(delta < (24*60*60)) {
r = '' + (parseInt(delta / 3600, 10)).toString() + ' hours ago';
} else if(delta < (48*60*60)) {
r = 'a day ago';
} else {
r = (parseInt(delta / 86400, 10)).toString() + ' days ago';
}
return 'about ' + r;
};
The problem appears to be you are using the function before you define it. This is why you are receiving the undefined error. Try changing the order so that the function is evaluated last.