JavaScript Tutorial
JavaScript Dates
new Date() is now. Get the year, month, and day, then format them yourself for a clock or a stamp.
new Date() is now
new Date() builds a Date object for this instant on the machine that runs the script. Call it again a second later and you get a later time. There is no import.
Logging a Date prints a long string. For a clock or a filename stamp you read the pieces with getters and join them yourself.
Run these in /javascript/try. The preview shows whatever you write onto the page. The console shows the raw Date.
Year, month, and day
getFullYear() is the four-digit year. getDate() is the day of the month (1 to 31).getDay() is the weekday as a number (0 is Sunday). Those two names are easy to mix up.
getMonth() is zero-based: January is 0, December is 11. Add 1 when you display a calendar month.
Example
const now = new Date();
const year = now.getFullYear();
const month = now.getMonth() + 1;
const day = now.getDate();
console.log(now);
console.log(year);
console.log(month);
console.log(day);
document.body.textContent = year + "-" + month + "-" + day;
Hours, minutes, seconds
getHours() uses a 24-hour clock from 0 to 23. getMinutes() andgetSeconds() run from 0 to 59. A clock face usually wants two digits, so pad single digits with a leading zero.
Example
function twoDigits(n) {
return String(n).padStart(2, "0");
}
const now = new Date();
const clock =
twoDigits(now.getHours()) + ":" +
twoDigits(now.getMinutes()) + ":" +
twoDigits(now.getSeconds());
console.log(clock);
document.body.textContent = clock;
Build a stamp
ISO-style stamps are year, month, day, then time, with hyphens and colons. You control the order. JavaScript will not invent your product’s date format for you.
Example
function twoDigits(n) {
return String(n).padStart(2, "0");
}
const now = new Date();
const stamp =
now.getFullYear() + "-" +
twoDigits(now.getMonth() + 1) + "-" +
twoDigits(now.getDate()) + " " +
twoDigits(now.getHours()) + ":" +
twoDigits(now.getMinutes());
console.log(stamp);
document.body.textContent = stamp;
Specific dates and timestamps
new Date(year, monthIndex, day) builds a local date. Remember the month index is still zero-based: new Date(2026, 0, 1) is 1 January 2026.
getTime() returns milliseconds since 1 January 1970 UTC. Subtract two timestamps to get a duration in milliseconds. Divide by 1000 for seconds.
Example
const start = new Date(2026, 0, 1);
const now = new Date();
const ms = now.getTime() - start.getTime();
const days = Math.floor(ms / 1000 / 60 / 60 / 24);
console.log(start.toString());
console.log(ms);
console.log(days);
document.body.textContent = "days since 1 Jan 2026: " + days;
Getters you will use
| Call | Meaning |
|---|---|
getFullYear() | Four-digit year |
getMonth() | Month 0–11. Add 1 to display. |
getDate() | Day of the month 1–31 |
getDay() | Weekday 0–6. Sunday is 0. |
getHours() | Hour 0–23 |
getTime() | Milliseconds since 1970-01-01 UTC |
Date.parse and new Date("2026-01-01") depend on the string format and the timezone. Prefer numeric arguments when you know the year, month, and day.
What to remember
new Date()is the current instant.- Month is 0–11. Day-of-month is
getDate(), notgetDay(). - Format a clock or stamp by joining padded pieces.
getTime()is the value you subtract to measure duration.
Next: the Math object — round, floor, ceil, max, min, and abs, with nothing to import.