Step 10
It is time to calculate the sleep cycles. Let's focus on the algorithm:
- When the
zzz
button is clicked, we want to record the current time; - Allow 14 minutes to fall asleep;
- Create six 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()
It 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()
It returns a string with a locality-sensitive representation of the time portion of this date, based on system settings.
The keyword
prototype
indicatestoLocaleTimeString()
is an instance method (unlikenow()
which is a static method).
- Let's try the code below
let time = new Date(); console.log(time.toLocaleTimeString());
- So we can get the current time and print it out! Next, we will write JavaScript code to implement our algorithm.