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

CallMeaning
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(), not getDay().
  • 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.

FAQ: JavaScript Dates

Common questions about this page.

What is the StudyGrid JavaScript tutorial?

The StudyGrid JavaScript tutorial is a full beginner track: variables, functions, arrays, if/else, the DOM, events, storage, and fetch. Each chapter has copy-and-run examples.

Should I run javascript dates examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn javascript dates in this JavaScript JavaScript lesson (JavaScript Dates).

Is the JavaScript editor the same as Try Python?

No. Try JavaScript is a live page plus a console at /javascript/try. Try Python stays at /try. JavaScript lessons never open the Python editor.

Do I need to install anything to learn JavaScript?

No. Open a chapter, click Try it in JavaScript, and the page and console update in the browser.

Where should I start the JavaScript tutorial?

Start at JavaScript Intro, then Where To, Output, and Syntax. After variables and functions, continue to the DOM and events. Use Next at the bottom of each chapter.

Is the JavaScript tutorial free?

Yes. The JavaScript studio on StudyGrid (studygrid.in) is free: dashboard, chapters, and the live editor.