Log in

View Full Version : Date but displayed as words



ukstormer
06-21-2012, 12:14 PM
Following on from my previous post regards a clock as words (http://www.dynamicdrive.com/forums/showthread.php?t=69321) 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.


<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

jscheuer1
06-22-2012, 01:36 AM
It's looks sloppy and/or dated to me, but also serviceable.

ukstormer
06-22-2012, 08:36 AM
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.

jscheuer1
06-22-2012, 09:24 AM
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:


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

would be:


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.


var this_year = this_date_timestamp.getYear() //extract year

if (this_year < 1000)
this_year+= 1900; //fix Y2K problem

becomes:


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:


setTimeout(function(){location.reload(true);},timeoutPeriod);

But even that may be unnecessary, as simply updating the time might be sufficient.

ApacheTech
06-22-2012, 11:01 AM
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.

jscheuer1
06-22-2012, 12:57 PM
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.

ukstormer
06-22-2012, 01:36 PM
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

ApacheTech
06-22-2012, 05:45 PM
There's a good course here:

http://www.javacommerce.com/displaypage.jsp?name=index.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.