Learn TypeScript in Construct, part 11: Standard library

UpvoteUpvote 3 DownvoteDownvote

Index

Features on these Courses

Stats

159 visits, 174 views

Tools

Translations

This tutorial hasn't been translated.

License

This tutorial is licensed under CC BY-NC 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Published on 30 Jul, 2025. Last updated 31 Jul, 2025

Date

The Date object provides methods for dealing with dates and times.

In computing, dates and times are often internally represented as the number of milliseconds since January 1, 1970 00:00:00 UTC. This is a somewhat arbitrary choice, but lots of computer systems use this method, as it provides millisecond precision and is consistent internationally, avoiding the need to take in to account geographic time zones. It derives from old UNIX-based computer systems, so the January 1 1970 date is often referred to as the "UNIX epoch". For example at time of writing on 30th July 2025, the current time is 1753879503447 milliseconds since the UNIX epoch.

When displaying dates as strings, it will use the local language and formatting style, as well as the local timezone, based on your system settings. So note the particular format shown in the examples below may differ from what you see.

Try out the following lines of code in the browser console.

// Try entering in to the browser console:

// Using just Date() returns a string of the current date and time
Date()		// Wed Jul 30 2025 13:46:06 GMT+0100 (British Summer Time)

// Date.now() provides the current time in milliseconds since the UNIX epoch
Date.now()	// 1753879577364

// new Date() creates a new object representing the
// current date. Note while this creates an object,
// the console will show it as a date and time.
new Date()	// Wed Jul 30 2025 13:46:27 GMT+0100 (British Summer Time)

// new Date(time) creates a Date object using the
// time in milliseconds since the UNIX epoch.
new Date(1753879577364)	// Wed Jul 30 2025 13:46:17 GMT+0100 (British Summer Time)

// new Date(year, monthIndex), with optional additional parameters up to
// new Date(year, monthIndex, day, hours, minutes, seconds, milliseconds)
// creates a Date object with the given date and time.
// NOTE: 'monthIndex' is 0-based (0 being January), but 'day' is 1-based.

// Tue Jul 01 2025 00:00:00 GMT+0100 (British Summer Time)
new Date(2025, 6)

// Wed Jul 30 2025 00:00:00 GMT+0100 (British Summer Time)
new Date(2025, 6, 30)	

// Wed Jul 30 2025 13:47:12 GMT+0100 (British Summer Time)
new Date(2025, 6, 30, 13, 47, 12)

Here is a code sample you can try in Construct that demonstrates a few more methods of Date objects to get the day, month and year of the current date as numbers (but note the month is zero-based, so we add 1 to it to start with January as 1 instead of 0).

// Get a Date object representing the current time
let currentDate = new Date();

// Log the day, month and year
// NOTE: the month is zero-based (0 is January) so we add 1 to it
console.log(`The day is ${currentDate.getDate()}`);
console.log(`The month is ${currentDate.getMonth() + 1}`);
console.log(`The year is ${currentDate.getFullYear()}`);

There's lots more you can do with Date in TypeScript. See the Date MDN documentation for more.

  • 0 Comments

Want to leave a comment? Login or Register an account!