Step 8

We are content with the content and styling (pun intended!). Let's focus on the algorithm:

  • When the zzz button is clicked we want to record the current time
  • Allow 14 minutes to fall sleep
  • Create 6 cycles of 90 minutes each
  • Display the cycles as suggested wake up times.

The date object

  • Let's see how we can get the current time (date and time); click on the play button below and "run" the code snippet
let time = Date.now();
console.log(time);
  • Note the output is an integer! Let's find out what this value is; Google: MDN JavaScript Date. Follow the first suggested result and read the docs!

JavaScript Date objects represent a single moment in time in a platform-independent format. Date objects contain a number that represents milliseconds since 1/1/1970 UTC.

Further notice the following methods:

Date.now()

Returns the numeric value corresponding to the current time; the number of milliseconds elapsed since 1/1/1970 00:00:00 UTC, with leap seconds ignored.

Date.prototype.toLocaleTimeString()

Returns a string with a locality-sensitive representation of the time portion of this date, based on system settings.

The keyword prototype indicates toLocaleTimeString() is an instance method (unlike now() which is a static method).

  • Let's try the code below
let today = new Date();
console.log(today.toLocaleTimeString());
  • So we can get the current time and print it out! Next we will write JavaScript code to implement our algorithm.