TypeScript Tutorial

TypeScript Dates

Date measures time. Construct it, read parts, and format a stamp.

A moment with a type

Date is the built-in type for a point in time. You construct it, read year/month/day, and print a stamp. C++ chrono measures durations and clocks; this chapter is the calendar object JavaScript gives you. Types on StudyGrid are checked by tsc; the values are ordinary Date instances at run time.

new Date() uses the clock on the machine. That output changes every run. This tutorial constructs from a fixed ISO string so the printed lines stay the same on every compile.

Construct from an ISO string

Pass a string in ISO 8601 form: YYYY-MM-DDTHH:mm:ss.sssZ. The Z means UTC. Store the result in a Date variable. toISOString() prints that UTC stamp back out.

Example

const stamp = new Date("2026-08-19T15:30:00.000Z");
console.log(stamp.toISOString());
console.log(stamp.getTime());

First line is 2026-08-19T15:30:00.000Z. Second line is the millisecond offset from 1 January 1970 UTC, a fixed integer for this string. Use that integer when you need to subtract two instants.

Run this at /typescript/try. Change the hour in the string and watch both lines follow. Do not start from new Date() if you want output you can check by eye.

Read parts in UTC

getUTCFullYear, getUTCMonth, getUTCDate, getUTCHours read the UTC calendar. Month is zero-based: January is 0, August is 7. Add 1 when you print a human month number. Local getters (getHours without UTC) depend on the timezone of the machine, so they are unstable in a tutorial.

Example

const stamp = new Date("2026-08-19T15:30:00.000Z");
console.log(stamp.getUTCFullYear());
console.log(stamp.getUTCMonth() + 1);
console.log(stamp.getUTCDate());
console.log(stamp.getUTCHours());
console.log(stamp.getUTCMinutes());

Output is 2026, 8, 19, 15, 30. Those five numbers match the ISO string. They will not change if you run the file in another timezone.

Subtract two dates

getTime() returns milliseconds. Subtract two of them for a duration. Divide by 1000 for seconds, by 60000 for minutes. This is the same idea as C++ duration_cast, done with ordinary numbers.

Example

const start = new Date("2026-08-19T15:30:00.000Z");
const end = new Date("2026-08-19T15:32:30.000Z");
const ms = end.getTime() - start.getTime();
console.log(ms);
console.log(ms / 1000);
console.log(ms / 60000);

Output is 150000, then 150, then 2.5. Two minutes and thirty seconds live in the two ISO strings, not in the wall clock.

Invalid dates

A bad string still constructs a Date, but getTime() is NaN. Check withNumber.isNaN before you trust parts. Do not print NaN and call it a stamp.

Example

const good = new Date("2026-08-19T15:30:00.000Z");
const bad = new Date("not-a-date");
console.log(Number.isNaN(good.getTime()));
console.log(Number.isNaN(bad.getTime()));

Output is false then true. Only good is a real instant.

What to call

CallRole
new Date("...Z")A fixed UTC instant
toISOString()Stable stamp text
getUTCFullYear() and friendsCalendar parts in UTC
getTime()Milliseconds; subtract for a duration

Keep stamps boring

Construct from ISO, read UTC parts, subtract getTime(). Skip locale formatting and timezones until you need them. Next: static, which is about data shared by every object of a class, not about clocks.

FAQ: TypeScript Dates

Common questions about this page.

What is the StudyGrid TypeScript tutorial?

The StudyGrid TypeScript tutorial follows the same chapter rhythm as C++: syntax, types, input, loops, functions, classes, generics, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run typescript date 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 typescript date in this TypeScript TypeScript lesson (TypeScript Dates).

Is the TypeScript editor the same as Try Python or Try C++?

No. Try TypeScript type-checks with tsc at /typescript/try and shows stdout plus compiler messages. Try Python stays at /try. Try C++ stays at /cpp/try. TypeScript lessons never open those editors.

Do I need to install a compiler to learn TypeScript?

No. Open a chapter, click Try it in TypeScript, and compile in the browser. You can also download a .ts file and compile locally with tsc.

Where should I start the TypeScript tutorial?

Start at TypeScript Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open TypeScript Examples, then generics, Map, and arrow functions. Use Next at the bottom of each chapter.

Is the TypeScript tutorial free?

Yes. The TypeScript workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.