Click to See Complete Forum and Search --> : Date() prototype problems in Opera7?
PhilgB
05-09-2003, 07:47 AM
I used to have some long rediculous code to display the date the way I wanted. Then I wrote this and it works fine everywhere except Opera7. I havent tested the other opera browsers yet...
Date.prototype.dayNames = new Array("Sunday","Monday","Tuesday",...);
Date.prototype.monthNames = new Array("January","February",...);
Date.prototype.getLongDate = fGetLongDate;
function fGetLongDate() {
return this.dayNames[this.getDay()] + ", " + this.monthNames[this.getMonth()] + " " + this.getDate() + ", " + this.getFullYear();
}
var now = new Date();
Nothing is displayed at all, no errors and no date
Any thoughts?
khalidali63
05-09-2003, 09:07 AM
Here you go this should do it for you.
<script type="text/javascript">
Date.prototype.dayNames = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
Date.prototype.monthNames = new Array("January","February","March","April",
"May","June","July","August",
"September","October","November","December");
Date.prototype.getLongDate = function (){
return this.dayNames[this.getDay()] + ", " + this.monthNames[this.getMonth()] + " " + this.getDate() + ", " + this.getFullYear();
}
function Process(){
var now = new Date();
alert(now.getLongDate())
}
</script>
</head>
<body class="body">
<input type="Button" value="date" onclick="Process();"/>
</body>
The above is tested with NS6+,IE6+ and Opera7.10
Charles
05-09-2003, 09:28 AM
A couple of style points, but nothing that really matters.
1) It doesn't seem to make sense to me to give each Date object it's own copy of the same constants. I prefer to put my constants in the Class.
2) You can use anonymous arrays instead of the Array constructor.
2) You can use an anonymous function to avoid cluttering up the name space.
Date.DAY_NAMES = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
Date.MONTH_NAMES = ['January,', 'February,', 'March,', 'April,', 'May,', 'June,', 'July,', 'August,', 'September,', 'October,', 'November,', 'December,'];
Date.prototype.getLongName = function () {return [Date.DAY_NAMES[this.getDay()], this.getDate(), Date.MONTH_NAMES[this.getMonth()], this.getFullYear()].join(' ')};
PhilgB
05-09-2003, 11:55 AM
still nothing
im calling it like this
document.write(now.getLongDate());
but nothing shows up...
Khalid's code works for me in Opera 7.0
Charles
05-09-2003, 12:32 PM
And mine is working fine in Opera 7.10, though I ususlly shorten it to something like:
<script type="text/javascript">
<!--
Date.prototype.getLongName = function () {return [['Sunday,', 'Monday,', 'Tuesday,', 'Wednesday,', 'Thursday,', 'Friday,', 'Saturday,'] [this.getDay()], this.getDate(), ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] [this.getMonth()], this.getFullYear()].join(' ')}
document.write(new Date().getLongName())
// -->
</script>
khalidali63
05-09-2003, 01:42 PM
Originally posted by PhilgB
still nothing
im calling it like this
document.write(now.getLongDate());
but nothing shows up...
My recomendations.
1. try not to use document.write.
use a div element and set its text value using
element.innerHTML = now.getLongDate();
2.Since it works for other Gurus,m,ight you wanna check that JavaScript is enabled in your copy of opera??
3. still no sccess then you might wanna show us all of your code,to see where is the screw up...:D