Aral Code

Going Further Lesson 26 of 30

JavaScript Dates

Work with dates and times — create Date objects, get the year, month, day and time, format dates in Filipino and English, and compute ages and countdowns.

Clocks, birthdays, deadlines and "posted 3 days ago" all need dates. JavaScript handles them with the built-in Date object.

Creating dates

const now = new Date();                       // right now
const independence = new Date(1898, 5, 12);   // June 12, 1898 (month 5 = June!)
const fromText = new Date("2026-12-25");      // from an ISO date string

Getting parts of a date

MethodReturns
getFullYear()Year, like 2026
getMonth()Month, 0–11
getDate()Day of the month, 1–31
getDay()Day of the week, 0 (Sunday) – 6 (Saturday)
getHours(), getMinutes(), getSeconds()Time parts
getTime()Milliseconds since January 1, 1970
Example Today in Filipino
<p id="out"></p>
<script>
  const days = ["Linggo", "Lunes", "Martes", "Miyerkules", "Huwebes", "Biyernes", "Sabado"];
  const months = ["Enero", "Pebrero", "Marso", "Abril", "Mayo", "Hunyo",
                  "Hulyo", "Agosto", "Setyembre", "Oktubre", "Nobyembre", "Disyembre"];
  const d = new Date();
  document.getElementById("out").textContent =
    `Ngayon ay ${days[d.getDay()]}, ${months[d.getMonth()]} ${d.getDate()}, ${d.getFullYear()}.`;
</script>

Formatting with toLocaleDateString

The browser can format dates for you, in many languages:

Example Built-in date formatting
<pre id="out"></pre>
<script>
  const d = new Date();
  document.getElementById("out").textContent =
    d.toLocaleDateString("en-PH", { dateStyle: "full" }) + "\n" +
    d.toLocaleDateString("fil-PH", { dateStyle: "full" }) + "\n" +
    d.toLocaleDateString("en-PH") + "\n" +
    d.toLocaleTimeString("en-PH", { hour: "numeric", minute: "2-digit" });
</script>

Date math

Subtracting two dates gives the difference in milliseconds. Divide to get days:

Example Days until Christmas
<p id="out" style="font-size:20px"></p>
<script>
  const today = new Date();
  let xmas = new Date(today.getFullYear(), 11, 25);
  if (today > xmas) xmas = new Date(today.getFullYear() + 1, 11, 25);
  const ms = xmas - today;
  const days = Math.ceil(ms / (1000 * 60 * 60 * 24));
  document.getElementById("out").textContent = `🎄 ${days} day(s) until Christmas!`;
</script>

1000 * 60 * 60 * 24 is the number of milliseconds in one day: 1000 ms × 60 seconds × 60 minutes × 24 hours.

Computing age

Example How old are you?
<label>Birthday: <input type="date" id="bday" value="2011-03-14"></label>
<p id="out"></p>
<script>
  function age(birth) {
    const today = new Date();
    let years = today.getFullYear() - birth.getFullYear();
    const hadBirthday =
      today.getMonth() > birth.getMonth() ||
      (today.getMonth() === birth.getMonth() && today.getDate() >= birth.getDate());
    if (!hadBirthday) years--;
    return years;
  }
  const input = document.getElementById("bday");
  const show = () => {
    const b = new Date(input.value + "T00:00");
    document.getElementById("out").textContent = `You are ${age(b)} years old.`;
  };
  input.addEventListener("input", show);
  show();
</script>

Just subtracting the years isn't enough: if your birthday hasn't come yet this year, you're one year younger. That's what hadBirthday checks.

Adding days

const due = new Date();
due.setDate(due.getDate() + 7);   // one week from today

setDate handles month and year changes automatically: adding 7 days to January 28 correctly gives February 4.

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. What does getMonth() return for March?
2. What does getDay() return?
3. Subtracting two Date objects gives…
4. Which adds 7 days to d?

By Sir Reyel · Last updated 2026-12-04