View Full Version : changing order of date format
johnwboyd
08-20-2008, 09:42 AM
This script writes the date like this: Wednesday, 20 July 2008
How can I change this to: Wednesday, July 20th 2008
Also I don't like the <h10> format. I just want to be able to set the size as 2 or in pixels. But when I tried to remove that it had an error.
<script type="text/javascript">
<!--
Date.prototype.toString = 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('<h10>', new Date(), '</h10>')
// -->
</script>
codeexploiter
08-20-2008, 10:05 AM
Check the code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled Document</title>
<style type="text/css">
</style>
<script type="text/javascript">
Array.prototype.inArray = function(value){
var i;
for (i = 0; i < this.length; i++) {
if (this[i] === value) {
return true;
}
}
return false;
};
Date.prototype.toString = function(){
var d = new Date().getDate();
var st = [1, 21, 31], rd = [3, 23], nd = [2, 22];
var h = (st.inArray(d) ? "st " : (rd.inArray(d) ? "rd " : (nd.inArray(d) ? "nd " : "th ")));
return [['Sunday,', 'Monday,', 'Tuesday,', 'Wednesday,', 'Thursday,', 'Friday,', 'Saturday,'][this.getDay()], ['January', 'February', 'March', 'April', 'May', 'June', , 'July', 'August', 'September', 'October', 'November', 'December'][this.getMonth()], this.getDate() + h, this.getFullYear()].join(' ')
}
document.write('<h10>', new Date(), '</h10>')
</script>
</head>
<body>
</body>
</html>
All the changes made by me in the script is highlighted
Hope this helps
johnwboyd
08-20-2008, 10:13 AM
Thanks a lot! If only I had asked sooner cus' I gotta adjust my layout now. Well at least not too many pages.
johnwboyd
08-20-2008, 10:23 AM
I figured out how to remove the extra space you had in between 20th and 2008:
Wednesday, July 20th 2008
<script type="text/javascript">
var d = new Date().getDate()
var st = "1,21,31";
var rd = "3,23"
var nd = "2,22";
var h;
if(st.indexOf(d)!= -1)
h = "st ";
else if(rd.indexOf(d)!= -1)
h = 'rd ';
else if(nd.indexOf(d)!= -1)
h = 'nd ';
else
h = 'th ';
Date.prototype.toString = function(){
return [['Sunday,', 'Monday,', 'Tuesday,', 'Wednesday,', 'Thursday,', 'Friday,', 'Saturday,'][this.getDay()], [' '], ['January', 'February', 'March', 'April', 'May', 'June', , 'July', 'August', 'September', 'October', 'November', 'December'] [this.getMonth()], [' '], this.getDate() + h, this.getFullYear()].join('')
}
document.write('<h10>', new Date(), '</h10>')
</script>
codeexploiter
08-20-2008, 10:27 AM
Sorry John the earlier version had some issues. I've rectified it here
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled Document</title>
<style type="text/css">
</style>
<script type="text/javascript">
Array.prototype.inArray = function(value){
var i;
for (i = 0; i < this.length; i++) {
if (this[i] === value) {
return true;
}
}
return false;
};
Date.prototype.toString = function(){
var d = new Date().getDate();
var st = [1, 21, 31], rd = [3, 23], nd = [2, 22];
var h = (st.inArray(d) ? "st " : (rd.inArray(d) ? "rd " : (nd.inArray(d) ? "nd " : "th ")));
return [['Sunday,', 'Monday,', 'Tuesday,', 'Wednesday,', 'Thursday,', 'Friday,', 'Saturday,'][this.getDay()], ['January', 'February', 'March', 'April', 'May', 'June', , 'July', 'August', 'September', 'October', 'November', 'December'][this.getMonth()], this.getDate() + h, this.getFullYear()].join(' ')
}
document.write('<h10>', new Date(), '</h10>')
</script>
</head>
<body>
</body>
</html>
johnwboyd
08-20-2008, 10:31 AM
Nope. That one still has an extra space after 20th. Try mine. Works in both browsers ;)
johnwboyd
08-20-2008, 10:34 AM
Wait a minute though. It's showing July here! It should be August. Oh oh.
codeexploiter
08-20-2008, 10:37 AM
That problem is due to an empty element in your original code you've posted in your first post (after July there is an empty element before August).
return [['Sunday,', 'Monday,', 'Tuesday,', 'Wednesday,', 'Thursday,', 'Friday,', 'Saturday,'][this.getDay()], ['January', 'February', 'March', 'April', 'May', 'June', , 'July', 'August', 'September', 'October', 'November', 'December'][this.getMonth()], this.getDate() + h, this.getFullYear()].join(' ')
The above code should be
return [['Sunday,', 'Monday,', 'Tuesday,', 'Wednesday,', 'Thursday,', 'Friday,', 'Saturday,'][this.getDay()], ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'][this.getMonth()], this.getDate() + h, this.getFullYear()].join(' ')
Now it works correctly
johnwboyd
08-20-2008, 11:40 AM
I figured out what I needed to change to remove the extra space after 20th (in red below). It was a space after "th":
var h = (st.inArray(d) ? "st " : (rd.inArray(d) ? "rd " : (nd.inArray(d) ? "nd " : "th")));
So the total script:
<script type="text/javascript">
Array.prototype.inArray = function(value){
var i;
for (i = 0; i < this.length; i++) {
if (this[i] === value) {
return true;
}
}
return false;
};
Date.prototype.toString = function(){
var d = new Date().getDate();
var st = [1, 21, 31], rd = [3, 23], nd = [2, 22];
var h = (st.inArray(d) ? "st " : (rd.inArray(d) ? "rd " : (nd.inArray(d) ? "nd " : "th")));
return [['Sunday,', 'Monday,', 'Tuesday,', 'Wednesday,', 'Thursday,', 'Friday,', 'Saturday,'][this.getDay()], ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'][this.getMonth()], this.getDate() + h, this.getFullYear()].join(' ')
}
document.write('<h10>', new Date(), '</h10>')
</script>
codeexploiter
08-20-2008, 12:04 PM
Yes you are correct if you don't need an extra space (I've done that accidentally) you need to remove the extra space that I've put in the below highlighted area:
var h = (st.inArray(d) ? "st " : (rd.inArray(d) ? "rd " : (nd.inArray(d) ? "nd " : "th ")));
Hope now this is clear and the script is working correctly in your page.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.