Learn JavaScript in Construct, part 10: Standard library

13

Index

Stats

7,829 visits, 18,542 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 26 Nov, 2021. Last updated 16 Dec, 2021

Date

The JavaScript 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 26th November 2021, the current time is 1637930545179 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()		// Fri Nov 26 2021 12:38:13 GMT+0000 (Greenwich Mean Time)

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

// 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()	// Fri Nov 26 2021 12:38:13 GMT+0000 (Greenwich Mean Time)

// new Date(time) creates a Date object using the
// time in milliseconds since the UNIX epoch.
new Date(1637930545179)	// Fri Nov 26 2021 12:38:13 GMT+0000 (Greenwich Mean 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.

// Mon Nov 01 2021 00:00:00 GMT+0000 (Greenwich Mean Time)
new Date(2021, 10)

// Tue Nov 23 2021 00:00:00 GMT+0000 (Greenwich Mean Time)
new Date(2021, 10, 23)	

// Tue Nov 23 2021 11:53:12 GMT+0000 (Greenwich Mean Time)
new Date(2021, 10, 23, 11, 53, 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 JavaScript. See the Date MDN documentation for more.

Next Tutorial In Course

Learn JavaScript in Construct, part 11: Construct APIs 18:03

Learn how to use Construct's built-in features - aka APIs - to access objects and more from JavaScript.

  • 5 Comments

  • Order by
Want to leave a comment? Login or Register an account!
  • awesome details! thanks Ashley!

  • Construct its not about codes !!! its about an easy form to create a game by simple drag and drop events and actions !!! let these codes for professionals of AAA companies !!! Shame on you !!!!

    • Construct is built on JS and can be extended with addons written in JS. That has been true since the release of C3. But nobody is forcing any user to learn JS to make games or apps with C3. This series is for people who want to expand their knowledge. No C3 user needs to understand JS to make software, but many of us would like to better understand the underlying language and be able to write addons.

      If your complaint is that it took time and energy to write these tutorials, and that this takes time and energy away from improving C3, then I understand. I wish that it was possible to create behaviors using nothing but event sheet logic, and I would love to see more time and energy directed at that. But that doesn't make this tutorial series a bad thing. As a long-time game designer with a good grounding in basic programming, this series is quite valuable to me. If anything it inspires me to push Construct further using JS.

  • One question that emoji with two characters isn't it because od this :D .???

  • Really good job...