Jump to content



Photo
- - - - -

Today.Js

chapter 6 time date

  • Please log in to reply
9 replies to this topic

#1 Max

Max

    Advanced Member

  • Members
  • PipPipPip
  • 66 posts
  • LocationSpain

Posted 4 May 2012 - 10:07 AM

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;
  • 0

#2 Antonio Conte

Antonio Conte

    Advanced Member

  • Members
  • PipPipPip
  • 1,061 posts
  • LocationOslo, Norway

Posted 4 May 2012 - 10:25 AM

I think this is because of how date and time objects work. You cannot add a zero to a timestamp and expect it to be the same. From experience in other languages, this must be done by formatting the date instead of changing the datetime itself.
  • 0

#3 Edward

Edward

    Advanced Member

  • Members
  • PipPipPip
  • 1,127 posts

Posted 4 May 2012 - 10:32 AM

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
  • 1

#4 HartleySan

HartleySan

    Advanced Member

  • Members
  • PipPipPip
  • 2,901 posts
  • LocationColumbus, OH USA

Posted 4 May 2012 - 12:43 PM

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.
  • 1

#5 Max

Max

    Advanced Member

  • Members
  • PipPipPip
  • 66 posts
  • LocationSpain

Posted 7 May 2012 - 9:24 AM

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!!
  • 1

#6 Paul Swanson

Paul Swanson

    Excellent Advisor

  • Members
  • PipPipPip
  • 163 posts
  • LocationPortland, OR, USA

Posted 7 May 2012 - 9:42 AM

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.
  • 0

#7 HartleySan

HartleySan

    Advanced Member

  • Members
  • PipPipPip
  • 2,901 posts
  • LocationColumbus, OH USA

Posted 7 May 2012 - 9:45 AM

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.mo...al_Objects/Date
  • 0

#8 Max

Max

    Advanced Member

  • Members
  • PipPipPip
  • 66 posts
  • LocationSpain

Posted 9 May 2012 - 8:06 AM

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!
  • 0

#9 Edward

Edward

    Advanced Member

  • Members
  • PipPipPip
  • 1,127 posts

Posted 9 May 2012 - 9:07 AM

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. ^_^
  • 0

#10 Max

Max

    Advanced Member

  • Members
  • PipPipPip
  • 66 posts
  • LocationSpain

Posted 9 May 2012 - 9:42 AM

Oh...and Monkey Island.

Edward - you lucky chap - being able to spend all day with php!
  • 0





Also tagged with one or more of these keywords: chapter 6, time, date