Jump to content
Larry Ullman's Book Forums

Recommended Posts

O.K. - stuck again!

 

Page 178 - chapter 6.

 

As a learning tool, I decided to edit 'today.js' so that when it displayed single digit minutes ((i.e. 0 - 9) it would insert a zero by way of formatting.

 

I have created a new variable 'todayMinutes' to which I add a zero if the number of minutes are less than ten.

 

The only problem is that it doesn't work!

 

I have substituted the zero for a text character to force the variable to a string without success.

 

Would appreciate some advice:...............

 

// today.js

// This script indicates the current date and time.

 

// Call this function when the page has loaded:

function init() {

 

// Want to be strict:

'use strict';

 

// Create a Date object:

var today = new Date();

if (today.getMinutes() < 10)

{

var todayMinutes = '0' + today.getMinutes();

}

// Create a custom message:

var message = 'Right now it is ' + today.toLocaleDateString();

message += ' at ' + today.getHours() + ':' + todayMinutes.value;

 

// Get a reference to the paragraph:

var output = document.getElementById('output');

 

// Update the innerText or textContent property of the paragraph:

if (output.textContent !== undefined) {

output.textContent = message;

} else {

output.innerText = message;

}

 

} // End of init() function.

 

window.onload = init;

Link to comment
Share on other sites

Ive just been testing it now you can add the zero to it and it creates a string, but if you remove the (')s the object will be treated as a number.

 

var todayMinutes = '0' + today.getMinutes(); Result of 029

 

var todayMinutes = 0 + today.getMinutes(); Result of 29

  • Upvote 1
Link to comment
Share on other sites

As far as I can tell, you're only declaring the todayMinutes variable when the number of minutes in the current time is less than 10. You need to declare the variable in all cases, and then edit it as necessary.

What would probably work is the following:

 

var todayMinutes = today.getMinutes();

if (todayMinutes < 10) {

 todayMinutes = '0' + todayMinutes;

}

 

Try that out, and please let us know. Thanks.

  • Upvote 1
Link to comment
Share on other sites

Hi there

 

Thanks for all your help, Edward and HartleySan. I think I have it now......

 

//Give the whole date string to variable 'today'

var today = new Date();

 

//find out if there is only one digit (i.e. 0 - 9)

if (today.getMinutes() < 10)

{

//create a variable (todayMinutes) to accept the number of minutes from 'today'

var todayMinutes = '0' + today.getMinutes();

}

else

//if there are 2 digits, don't do anything except pass the Nº minutes to 'todayMinutes'

{

var todayMinutes = today.getMinutes();

}

//Now todayMinutes is a variable (integer?..string?) between 00 and 59

// Create a custom message:

var message = 'Right now it is ' + today.toLocaleDateString();

//add the minutes variable to the text...

message += ' at ' + today.getHours() + ':' + todayMinutes;

 

// Get a reference to the paragraph:

var output = document.getElementById('output');

 

 

--------------------

 

However, this still leaves us with a problem....

 

without adding the zero to minutes (commenting out if/else and replacing it with 'var todayMinutes = today.getMinutes();'), the result is:....

 

'Right now it is 05 May 2012 at 3:8'.....

 

...and the variable 'today' equals 'Sat May 05 2012 03:02:47 GMT+0200 (Hora de verano romance)'. You will notice that the day, hour and minute are all preceded by a zero which suggests that js is converting the days as text and the hours and minutes as a numeric, otherwise 'today' would be 'Sat May 5 2012 3:2:47 GMT+0200 (Hora de verano romance)'.

 

O.K., the script works, but I would like to know why single days are passed to a variable with a preceding zero and hours and minutes aren't.

 

I have tried the following - it works but still doesn't supply the zero on the single digit minutes:.....

 

var todayMinutes = today.getMinutes().toString(); //Instead of the if/else conditional.

 

but still get:......Right now it is 05 May 2012 at 3:1

 

I know that this all may seem abstruse, but I feel that I'm missing something here.

 

By the way, Edward, your Avatar has brought back happy memories of playing Alone in the dark!!

  • Upvote 1
Link to comment
Share on other sites

Max, that's just how the javascript date object works. It doesn't zero-pad minutes (or seconds). For some reason, it does produce a 2-digit day of the month. To zero-pad minutes and seconds, you can create a simple function. Look at the W3Schools example here.

Link to comment
Share on other sites

Yeah, Edward's avatar and suggestion about gog.com totally made me buy the Alone in the Dark series instantly.

I just finished the game again earlier today for the first time in about 20 years, and the game still scared me as much now as when I was 9.

The game is truly creepy (and awesome).

 

Anyway, to answer your question, Max, I really don't know what you mean.

 

All of the following Date object methods will return single-digit numbers when less than 10:

getDate() - Gets the day of the month.

getHours() - Gets the hours.

getMinutes() - Gets the minutes.

 

As far as I can tell, all three methods return a number literal, which is never zero-padded without first converting it to a string.

Please see the following for details:

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date

Link to comment
Share on other sites

Hi Paul

 

Thanks for that - it's comforting to know that W3 and I came to more or less the same code, especially when I am having so much trouble absorbing all of the concepts in the book. (being 55 doesn't help). Am on the third pass!!! If it weren't for Larry's great writing style I would have given up by now.

 

Hi HartleySan

Yes..Alone in the Dark, Indiana Jones, Noctropolis - all great games. Don't have time nowadays, unfortunately!

Link to comment
Share on other sites

As i said in my profile my days are over for investigating mysterious mansions and killing zombies and various other monsters. I have now retired and would like to spend the rest of my life with PHP, MySQL and Javascript to build dynamic websites. The only ever detective work i want to do now is investigating bugs in my code or others that need help here that i can assist.

 

With Best Regards,

Edward Carnby. ^_^

Link to comment
Share on other sites

 Share

×
×
  • Create New...