Week 9. Basics of JavaScript

Intro to JavaScript



And then we’ll learn more about dates.

 

Dates in JavaScript & Microseasons Demo


I made this demo:

This demo does both:
( 1 ) shows the current date
( 2 ) shows the current microseason

Do check out the source code! 😎

I’ll try to share the most important parts!

Just to be upfront … dates in JavaScript can be a bit hairy. But I believe this is a pretty good demo that should get you going ~

 

Part 1 of JS


The first part of the JavaScript (at the end, right before </body>

var d = new Date().toLocaleString("en-US", {
  timeZone: "America/New_York",
  year: "numeric",
  month: "long",
  day: "numeric",
  hour: "numeric",
  minute: "numeric",
  timeZoneName: "short"
});

console.log(d);

var date_element = document.getElementById("date");
date_element.innerHTML = d;

Here, we are getting a new date element, calling it d — lines 1-9.

We’re logging it to the console — line 11.

Then in lines 13-14, we’re inserting that variable directly into the HTML, using the function innerHTML in the element with the id of date. (That is, <span id="date"></span> that exist in the HTML is where the date is being inserted.)