Skip to content

4.7 Structural Types Part 2

Date()

Task 1: Check the code below.

var dateNow = new Date();

console.log(`The date: ${dateNow}`);

The result is: The date: Sat Sep 05 2020 11:51:49 GMT+0300 (Eastern European Summer Time)

The Date Object

The Date constructor creates new date objects. The date object stores the current date and time.

To create a new data object using the Date() constructor, use the keyword new followed by Date().

Task 2: Refer to task 1; what is the constructor of dateNow.

var dateNow = new Date();

console.log(`The constructor is: ${dateNow.constructor}`);

The result is: The constructor is: function Date() { [native code] }

Task 3: What is the valueOf of date object?

var dateNow = new Date();

console.log(`dateNow.valueOf(): ${dateNow.valueOf()}`);

The result is: dateNow.valueOf(): 1599412703986


Current Time

Task 4: What is the current time? Hint: use Date.now()

console.log(`The time now is: ${Date.now()}`);

The result is: The time now is: 1599413110712

Task 5: What is the current time? Hint: use new Date().getTime().

var dateNow = new Date();

console.log(`dateNow.getTime(): ${dateNow.getTime()}`);

The result is: dateNow.getTime(): 1599413356554

Task 6: Check the result of the code below.

console.log(new Date().valueOf(), Date.now(), new Date().getTime())

The result is: 1599413475310 1599413475310 1599413475310

Info

new Date.valueOf(), Date.now() and new Date().getTime() returns the number of milliseconds elapsed since January 1, 1970.


Date Getters

Task 7: What is the current day of the month? Hint: use new Date().getDate().

var dateNow = new Date();
var dayOfMonth = dateNow.getDate();

console.log(`The full date is: ${dateNow}`);
console.log(`The day number is: ${dayOfMonth}`);

The result is as follows:

The full date is: Sun Sep 06 2020 20:38:11 GMT+0300 (Eastern European Summer Time)
The day number is: 6

new Date().getDate()

new Date().getDate() returns the day of the month (1-31) for the specified date according to the local time.

Task 8: What is the current day of the week? Hint: use new Date().getDay().

var dateNow = new Date();
var dayOfWeek = dateNow.getDay();

console.log(`The full date is: ${dateNow}`);
console.log(`The day number is: ${dayOfWeek}`);

The result is as follows:

The full date is: Sun Sep 06 2020 20:41:56 GMT+0300 (Eastern European Summer Time)
The day number is: 0

new Date().getDay()

new Date().getDay() returns the day of the week (0-6) for the specified date according to the local time.

Note: The first day of the week at my local time is Sunday. Today is Sunday, so new Date().getDay() returns 0. This result may vary on your end.

Task 9: What is the 4 digit year of the current date? Hint: use new Date().getFullYear().

var dateNow = new Date();
var fullYear = dateNow.getFullYear();

console.log(`The full date is: ${dateNow}`);
console.log(`The 4 digit year is: ${fullYear}`);

The result is as follows:

The full date is: Sun Sep 06 2020 20:48:45 GMT+0300 (Eastern European Summer Time)
The 4 digit year is: 2020

new Date().getFullYear()

new Date().getFullYear() returns the 4 digit year in the specified date according to local time.

Task 10: What is the 2-3 digit year of the current date? Hint: use new Date().getYear().

var dateNow = new Date();
var year = dateNow.getYear();

console.log(`The full date is: ${dateNow}`);
console.log(`The 2-3-digit year is: ${year}`);

The result is as follows:

The full date is: Sun Sep 06 2020 20:50:13 GMT+0300 (Eastern European Summer Time)
The 2-3-digit year is: 120

new Date().getYear()

new Date().getYear() returns the 2-3 digit year in the specified date according to local time.

Task 11: What is the current day of the month, the current day of the week, and the current year?

var date = new Date();

console.log(`The current day of the month: ${date.getDate()}`);
console.log(`The current day of the week: ${date.getDay()}`);
console.log(`The current year: ${date.getFullYear()}`);

The result is as follows:

The current day of the month: 6
The current day of the week: 0
The current year: 2020

Time Getters

Task 12: What is the current hour? Hint: use new Date().getHours().

var hours = new Date().getHours();

console.log(`The current hour: ${hours}`);

The result is: The current hour: 21

new Date().getHours()

new Date().getHours() returns the hour (0–23) in the specified date according to local time.

Task 13: What is the current minute? Hint: new Date().getMinutes().

var minutes = new Date().getMinutes();

console.log(`The current minute: ${minutes}`);

The result is: The current minute: 9

new Date().getMinutes()

new Date().getMinutes() returns the minutes (0–59) in the specified date according to local time.

Task 14: What is the current second? Hint: new Date().getSeconds().

var seconds = new Date().getSeconds();

console.log(`The current second: ${seconds}`);

The result is: The current second: 16

new Date().getSeconds()

new Date().getSeconds() returns the seconds (0–59) in the specified date according to local time.

Task 15: What is the current hour, minute, and second?

var date = new Date();
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();

console.log(`The date: ${date}`);
console.log(`The current hour: ${hours}`);
console.log(`The current minute: ${minutes}`);
console.log(`The current second: ${seconds}`);

The result is as follows:

The date: Sun Sep 06 2020 21:22:25 GMT+0300 (Eastern European Summer Time)
The current hour: 21
The current minute: 22
The current second: 25

Date Setters

Task 16: Create a new date object, and set the day of the month to 5. Hint: use new Date().setDate(5).

var date = new Date();
date.setDate(5);

console.log(date.getDate());

The result is: 5

Task 17: Refer to task 16; set the month to 10. Hint: use new Date().setMonth(10).

var date = new Date();
date.setDate(5);
date.setMonth(10);

console.log(date.getMonth());

The result is: 10

Task 18: Refer to task 17; set the year to 1998. Hint: use new Date().setFullYear(1998), or new Date().setYear(98)

var date = new Date();
date.setDate(5);
date.setMonth(10);
date.setFullYear(1998);

console.log(date.getFullYear());

The result is: 1998


Time Setters

Task 19: Refer to task 18; set the hour to 22. Hint: use new Date().setHours(22).

var date = new Date();
date.setDate(5);
date.setMonth(10);
date.setFullYear(1998);
date.setHours(22);

console.log(date.getHours());

The result is: 22

Task 20: Refer to task 19; set the minute to 50. Hint: use new Date().setMinutes(50).

var date = new Date();
date.setDate(5);
date.setMonth(10);
date.setFullYear(1998);
date.setHours(22);
date.setMinutes(50);

console.log(date.getMinutes());

The result is: 50

Task 21: Refer to task 20; set the second to 11. Hint: use new Date().setSeconds(11).

var date = new Date();
date.setDate(5);
date.setMonth(10);
date.setFullYear(1998);
date.setHours(22);
date.setMinutes(50);
date.setSeconds(11);

console.log(date.getSeconds());
console.log(date);

The result is as follows:

11
Thu Nov 05 1998 22:50:11 GMT+0200 (Eastern European Standard Time)

Extra Practice

Task 22: Create a new date object with the date: 12-05-1980 14:17:02.

var date = new Date();

date.setDate(12);
date.setMonth(5);
date.setYear(80);
date.setHours(14);
date.setMinutes(17);
date.setSeconds(2);

console.log(date);

The result is: Thu Jun 12 1980 14:17:02 GMT+0200 (Eastern European Standard Time)

Task 23: Refer to task 22; Display the day of the month, the month, the year, the hour, the minute, and the second in the console.

var date = new Date();

date.setDate(12);
date.setMonth(5);
date.setYear(80);
date.setHours(14);
date.setMinutes(17);
date.setSeconds(2);

console.log(date.getDate());
console.log(date.getMonth());
console.log(date.getYear());
console.log(date.getHours());
console.log(date.getMinutes());
console.log(date.getSeconds());

The result is as follows:

12
5
80
14
17
2

Task 24: Create a new date object with the date: 01-11-2758 01:02:00.

var date = new Date();

date.setDate(01);
date.setMonth(11);
date.setYear(2758);
date.setHours(01);
date.setMinutes(11);
date.setSeconds(00);

console.log(date);

The result is: Mon Dec 01 2758 01:11:00 GMT+0200 (Eastern European Standard Time)

Task 25: Refer to task 24; Display the day of the month, the month, the year, the hour, the minute,Task 25 and the second in the console.

var date = new Date();

date.setDate(01);
date.setMonth(11);
date.setYear(2758);
date.setHours(01);
date.setMinutes(11);
date.setSeconds(00);


console.log(date.getDate());
console.log(date.getMonth());
console.log(date.getFullYear());
console.log(date.getHours());
console.log(date.getMinutes());
console.log(date.getSeconds());

The result is as follows:

1
11
2758
1
11
0