Tokyo Time

 

5 points

 

Introduction

This app figures out and displays the date and time in New Jersey and in Tokyo, Japan. We'll use the Date class, which we used for an earlier program. The Date object uses military time, and so does this program. Tokyo is 14 hours ahead of New Jersey, so for example if it's 9:00 in New Jersey, it's 23:00 in Tokyo. If it's 23:00 in New Jersey, it's 13:00 - the following day - in Tokyo.

Recall that the Date class has these GET methods that can be useful:

Here's an example:
var nowNJ = new Date();
var date = nowNJ.getDate();

The Date class also has these SET methods that you can use to change the date:

Here's an example:
date.setMonth(1);
This code would set the date's month to February.

Here is an example from code.org that shows the Date class in action:

// printTime() is a function that formats the time

var nowNJ = new Date();
var minutes = nowNJ.getMinutes();
msg = "New Jersey date & time: \n" + printTime(nowNJ) + "\n";
minutes = minutes + 60;
nowNJ.setMinutes(minutes);
msg = msg + "New Jersey date & time: \n" + printTime(nowNJ) + "\n";
setText("lblResult", msg);

Here is the output from this code:

What this shows is that the Date class is smart. If you add minutes, hours or days to a Date object, the object will adjust correctly.

 

In the app, when you click the button, the app figures out and displays the current time in both New Jersey and Tokyo.

 

Extra Credit (3 points)

If it's 3 minutes after 2 a.m., this time could be display as 2:3. If you write the extra-credit, this time would display as 02:03. If either the hour or minute or both is a single digit, a "0" is displayed before the single digit. You could write a function to do this. The function could accept a Date object as a parameter. IF statements could be used to add a "0" when needed. Finally, the function could return a string with the time. The basic form of the function could be:
function formatTime(date)
{
   ....
   ....
   return msg;
}

The code could send the New Jersey Date object into the function, and then send the Tokyo Date object in.

 

Video

The video shows what a completed app should do. The video includes the extra-credit option.