Results 1 to 10 of 10

Thread: changing order of date format

  1. #1
    Join Date
    Feb 2008
    Posts
    83
    Thanks
    26
    Thanked 1 Time in 1 Post

    Question changing order of date format

    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('&nbsp;')}

    document.write('<h10>', new Date(), '</h10>')
    // -->
    </script>

  2. #2
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Check the code:

    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('&nbsp;')
                }
                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
    Last edited by codeexploiter; 08-20-2008 at 10:30 AM. Reason: Corrections

  3. The Following User Says Thank You to codeexploiter For This Useful Post:

    johnwboyd (08-20-2008)

  4. #3
    Join Date
    Feb 2008
    Posts
    83
    Thanks
    26
    Thanked 1 Time in 1 Post

    Default

    Thanks a lot! If only I had asked sooner cus' I gotta adjust my layout now. Well at least not too many pages.

  5. #4
    Join Date
    Feb 2008
    Posts
    83
    Thanks
    26
    Thanked 1 Time in 1 Post

    Default I'm learning!

    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()], ['&nbsp;'], ['January', 'February', 'March', 'April', 'May', 'June', , 'July', 'August', 'September', 'October', 'November', 'December'] [this.getMonth()], ['&nbsp;'], this.getDate() + h, this.getFullYear()].join('')
    }
    document.write('<h10>', new Date(), '</h10>')
    </script>

  6. #5
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Sorry John the earlier version had some issues. I've rectified it here

    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('&nbsp;')
                }
                document.write('<h10>', new Date(), '</h10>')
            </script>
        </head>
        <body>
        </body>
    </html>

  7. #6
    Join Date
    Feb 2008
    Posts
    83
    Thanks
    26
    Thanked 1 Time in 1 Post

    Default

    Nope. That one still has an extra space after 20th. Try mine. Works in both browsers

  8. #7
    Join Date
    Feb 2008
    Posts
    83
    Thanks
    26
    Thanked 1 Time in 1 Post

    Question

    Wait a minute though. It's showing July here! It should be August. Oh oh.

  9. #8
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    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).

    Code:
    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('&nbsp;')
    The above code should be

    Code:
    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('&nbsp;')
    Now it works correctly

  10. The Following User Says Thank You to codeexploiter For This Useful Post:

    johnwboyd (08-20-2008)

  11. #9
    Join Date
    Feb 2008
    Posts
    83
    Thanks
    26
    Thanked 1 Time in 1 Post

    Default

    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('&nbsp;')

    }
    document.write('<h10>', new Date(), '</h10>')
    </script>

  12. #10
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    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:

    Code:
    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.

  13. The Following User Says Thank You to codeexploiter For This Useful Post:

    johnwboyd (08-20-2008)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •