1.12 - Dates and times

We’ve used code that used date and time functionality in a couple of exercises already. Working with dates and times is a fairly common task for a program and there are a bunch of useful Javascript commands we can use for this.

let today = new Date();

console.log("The date is",today.toLocaleDateString());
console.log("The time is",today.toLocaleTimeString());

let epoch = today.getTime() / 1000;
console.log("The number of seconds since midnight 01/01/1970 is",epoch)
/*
Computer programs internally use this unit of time for a lot of purposes, you will see it come up again and again. It is useful for you to understand that the number of seconds since 01/01/1970 is significant as you will see it again and again. Curiously, in the case of Javascript, the getTime() function provides the epoch as milliseconds, hence the division by 1000. There is also a corresponding setTime() function you can use as shown below.
*/

When we create a new date variable (more technically the Date is known as an object, a distinction we’ll worry about later), there are a few different ways we can instantiate it (create it).

So, let’s practice creating datetime objects that aren’t just the current date. For instance, do you know what day of the week your birthday was? Let’s find out!

let birthday_text = prompt("What is your birthday (write it as dd/mm/yyyy) ?")
let parts = birthday_text.split("/");
let birthday_date = new Date(parts[2], parts[1]-1, parts[0]);  // year, month-1, day, hours, minutes, seconds
let days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
console.log("I understood that as",birthday_date.toLocaleDateString());
console.log("Which was, by the way, a", days[ birthday_date.getDay() ] );

Differences between dates

let birthday_text = prompt("What is your birthday (write it as dd/mm/yyyy) ?")
let parts = birthday_text.split("/");
let birthday_date = new Date(parts[2], parts[1]-1, parts[0]);  // year, month-1, day, hours, minutes, seconds
let today = new Date();
let milliseconds_per_day = 24 * 60 * 60 * 1000;
let difference = today - birthday_date;
let days_old = Math.trunc( difference / milliseconds_per_day );
console.log("You are ",days_old," days old!")

let five_thousand = new Date(birthday_date.getTime() + milliseconds_per_day*5000);
let ten_thousand = new Date(birthday_date.getTime() + milliseconds_per_day*10000);

if (five_thousand < today) {
   console.log("Did you know your 5'000th day was",five_thousand.toLocaleDateString());
} else {
   console.log("Did you know your 5'000th day will be",five_thousand.toLocaleDateString());
}
if (ten_thousand < today) {
   console.log("Did you know your 10'000th day was",ten_thousand.toLocaleDateString());
} else {
   console.log("Did you know your 10'000th day will be",ten_thousand.toLocaleDateString());
}

Numeric values from dates and times

In addition to working with converting dates to strings, we can also extract the numeric values for each component of a date or time.

let birthday_text = prompt("What is your birthday (dd/mm/yyyy)?");
let parts = birthday_text.split("/");
let dob = new Date(parts[2], parts[1]-1, parts[0]);
let today = new Date();
let this_year_bday = new Date(today.getFullYear( ), dob.getMonth(), dob.getDay());
let age_this_year = today.getFullYear() - dob.getFullYear();
let diff = (today - this_year_bday) / (24*60*60*1000);
if (diff > 0) {
    console.log("Your most recent birthday was ",diff," days ago. You turned ",age_this_year);
} else if (diff < 0) {
    console.log("Your next birthday is ",diff," days ago. You will turn ",age_this_year);
} else {
    console.log("It's your birthday! Happy birthday! You are ",age_this_year);
}

For the record, the following are the key functions for the date object you should have in your repertoire, assuming a date object called today:

Setting date/time values

Getting date/time values

Getting date/time strings