Results 1 to 8 of 8

Thread: Date but displayed as words

  1. #1
    Join Date
    Jun 2012
    Location
    Lancashire
    Posts
    31
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Date but displayed as words

    Following on from my previous post regards a clock as words I thought I would finish off the date and time theme with the date displayed as words, I have not written this code but as I have previously mentioned I am not a java programmer but enjoy mashing others code up to suit.

    The text can be easily styled and provision for the year to be viewed also in there, but I did not want that on my screen. Hope this can be of use to others and any constructive criticism would be appreciated.

    Code:
    <head>
    <title>date in words</title>
    <script language="Javascript">
    <!--
    function timedRefresh(timeoutPeriod) {
    	setTimeout("location.reload(true);",timeoutPeriod);
    }
    //   -->
    var this_date_words_array = new Array ("","first","second","third","fourth","fifth","sixth","seventh","eighth","ninth","tenth","eleventh","twelfth","thirteenth","fourteenth","fifteenth","sixteenth","seventeenth","eighteenth","nineteenth","twentieth","twenty-first","twenty-second","twenty-third","twenty-fourth","twenty-fifth","twenty-sixth","twenty-seventh","twenty-eighth","twenty-ninth","thirtieth","thirty-first")//date as words
    var this_month_name_array = new Array("january","february","march","april","may","june","july","august","september","october","november","december") //month names
    
    var this_date_timestamp=new Date()	//get current day-time stamp
    
    var this_date_words = this_date_timestamp.getDate()	//extract day of month
    var this_month = this_date_timestamp.getMonth()	//extract month
    var this_year = this_date_timestamp.getYear()	//extract year
    
    if (this_year < 1000)
    this_year+= 1900;	//fix Y2K problem
    
    var this_date_string = '<span class="emphasis">' +  this_date_words_array[this_date_words] + '</span>' + '<span class="basic">' + ' of ' + '</span>' + '<span class="emphasis">' + this_month_name_array[this_month] + '</span>' //concat long date string</p>
    </script>
    
    <style type="text/css">
            
            span.basic 
            {
                font-family:ariel; 
                color:#702342;
                font-weight:normal;
                font-size: 20px;
            }
          
             span.emphasis 
            {
                font-family:ariel; 
                color:#702342;
                font-size: 40px;
            }
          
        </style>
    
    </head>
    <body onload="JavaScript:timedRefresh(3600000);">
    <div align="left">
    <script language="JavaScript">document.write(this_date_string)</script>
    </div>
    </body>
    Regards

    UKStormer
    Last edited by jscheuer1; 06-22-2012 at 01:27 AM. Reason: Format

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    It's looks sloppy and/or dated to me, but also serviceable.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jun 2012
    Location
    Lancashire
    Posts
    31
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jscheuer1 View Post
    It's looks sloppy and/or dated to me, but also serviceable.
    I admitted to not being a programmer and welcomed constructive criticism, saying it is sloppy and dated is hardly constructive and a very negative attitude to take with a new member, if it is sloppy then try being helpful and tell me where the coding could be improved as other members have done with my other post.

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Ah, but saying, "also serviceable" isn't negative. And I could elaborate. It basically means that in current browsers and for the foreseeable future it should be OK.

    If you want to learn proper coding practices, I can give you some clues as well -

    The language attribute is deprecated for script tags, and unless you're using HTML 5, the type attribute is required for a script tag.

    Use of <!-- and // --> in script code blocks is unnecessary and can lead to errors. But be careful when removing them, make sure to get rid of all of them as missing some might also create errors.

    When declaring an array, it's more efficient to use the Array literal. For instance:

    Code:
    var this_month_name_array = new Array("january","february","march","april","may","june","july","august","september","october","november","december") //month names
    would be:

    Code:
    var this_month_name_array = ["january","february","march","april","may","june","july","august","september","october","november","december"] //month names
    There's at least one other array that could benefit from this efficiency.

    The getYear() method of the date object is deprecated in favor of the getFullYear() method, which requires no Y2K correction.

    Code:
    var this_year = this_date_timestamp.getYear()	//extract year
    
    if (this_year < 1000)
    this_year+= 1900;	//fix Y2K problem
    becomes:

    Code:
    var this_year = this_date_timestamp.getFullYear()	//extract year
    There are numerous global variables. On the plus side these generally have long names that would be unlikely to conflict with other scripts. But they might. It would be better to construct the code so that theese variables are local to the scope that requires them.

    Use of a string to represent a function in the setTimeout constructor is another area where errors can creep in. Here's an alternative:

    Code:
    setTimeout(function(){location.reload(true);},timeoutPeriod);
    But even that may be unnecessary, as simply updating the time might be sufficient.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Apr 2012
    Location
    Chester, Cheshire
    Posts
    329
    Thanks
    7
    Thanked 35 Times in 35 Posts

    Default

    One other thing. Because you're using Javascript, you don't need to reload the page to change the displayed HTML. You can navigate the DOM Tree and edit HTML on the fly. This means you can have your timed "refresh" without ever causing the page to re-render.

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Yes. I was alluding to that at the end of my last post. If you make those other changes, don't worry about the globals just yet, we can make it so it exposes only one or perhaps no globals and doesn't require reloading the page.

    But, if part of the purpose is to reload the page - that is if there's other content on the page that might be updated in the meantime . . . Well then, reloading the page should remain.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Jun 2012
    Location
    Lancashire
    Posts
    31
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Many thanks to both of you for your input, it is much appreciated, I will have another look at the code and take on board what you have said and try to re work it, as I have said I mainly like to play around with existing code to get it to look like I would want, I would love to do a java course to better understand everything, and maybe one day I will, but until then I will do my best to muddle through with the help of others better qualified than myself.

    Once again thank you

    UKStormer

  8. #8
    Join Date
    Apr 2012
    Location
    Chester, Cheshire
    Posts
    329
    Thanks
    7
    Thanked 35 Times in 35 Posts

    Default

    There's a good course here:

    http://www.javacommerce.com/displayp...x.sql&id=18222

    But, overall, it's about honing your own style and learning to code in your own way. As I'm sure you've seen on this site, there are many ways to do things and many approaches to take to the same problem.

    One piece of advice I would give. I stayed away from JQuery for a long time and now I'm finding that where not knowing it isn't exactly holding me back, it's definitely not making things better. Once you've gained the fundamental knowledge of JS, have a serious look at JQ.

    Same goes with the Server Side languages. I stayed with ASP.NET for so long because it was easy that I'm finding it much harder now to pick up PHP.

    Keep your knowledge as broad as you can and don't get tunnel vision just because something works.

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
  •