Skip to content

Commit 30e76f9

Browse files
feat(curriculum): add interactive examples to JS Date object lesson (freeCodeCamp#63925)
1 parent bc46ae9 commit 30e76f9

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

curriculum/challenges/english/blocks/lecture-working-with-dates/6733aafb9c0802f66cc1e056.md

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ challengeType: 19
55
dashedName: how-does-the-javascript-data-object-work-and-what-are-some-common-methods
66
---
77

8-
# --description--
8+
# --interactive--
99

1010
Dates and times in JavaScript have not always been easy to work with. In a professional codebase, you will probably be using a library that has solved a lot of those issues for you.
1111

@@ -23,25 +23,31 @@ The `new` keyword is used to create a new instance of the `Date` object, and the
2323

2424
Here is an example response you would see if you logged the value of `now` to the console:
2525

26+
:::interactive_editor
27+
2628
```js
2729
const now = new Date();
2830
console.log(now);
29-
// Mon Mar 15 2024 14:30:00 GMT-0700 (Pacific Daylight Time)
3031
```
3132

33+
:::
34+
3235
For the time, it is using the military time format, so `14:30` is `2:30 PM`. `GMT-0700` is the timezone offset, and `Pacific Daylight Time` is the timezone name.
3336

3437
You can also pass in a specific date and time to the `Date` object by providing the year, month, day, hour, minute, second, and millisecond values as arguments.
3538

3639
Here is an example of creating a new `Date` object with a specific date and time:
3740

41+
:::interactive_editor
42+
3843
```js
3944
const specificDate = new Date("July 4, 1776 12:00:00");
4045

4146
console.log(specificDate);
42-
// Wed Jul 04 1776 12:00:00 GMT-0700 (Pacific Daylight Time)
4347
```
4448

49+
:::
50+
4551
This is useful when you need to work with a specific date and time rather than the current date and time. The input always needs to be a string, and the format should be recognizable by the `Date` object.
4652

4753
To get the current date and time, you can use the `Date.now()` method, which returns the number of milliseconds since `January 1, 1970, 00:00:00 UTC`. This is known as the Unix epoch time.
@@ -50,37 +56,47 @@ Unix epoch time is a common way to represent dates and times in computer systems
5056

5157
If you need to get a day of the month based on the current date, you can use the `getDate` method. Here is an example of using the `getDate` method:
5258

59+
:::interactive_editor
60+
5361
```js
5462
const now = new Date();
5563
const date = now.getDate();
56-
console.log(date); // 15
64+
console.log(date);
5765
```
5866

67+
:::
68+
5969
In this example, we create a new date instance using the `Date` object and assign it to the variable `now`. We then call the `getDate` method on the `now` object to get the day of the month and assign it to the variable `date`. Finally, we log the value of `date` to the console, which will output the current day of the month.
6070

6171
`getDate` will return an integer value between `1` and `31`, depending on the day of the month. If the date is invalid, it will return `NaN` (Not a Number).
6272

6373
To get the month, you can use the `getMonth` method like this:
6474

75+
:::interactive_editor
76+
6577
```js
6678
const now = new Date();
6779
const month = now.getMonth();
6880
console.log(month);
69-
// 2
7081
```
7182

72-
The month is zero-based, so `January` is `0`, `February` is `1`, and so on. In this example, the output is `2`, which corresponds to `March`. If the month is invalid, it will return `NaN`.
83+
:::
84+
85+
The month is zero-based, so `January` is `0`, `February` is `1`, and so on. If the month is invalid, it will return `NaN`.
7386

7487
If you need to get the full year, then you can use the `getFullYear` method like this:
7588

89+
:::interactive_editor
90+
7691
```js
7792
const now = new Date();
7893
const year = now.getFullYear();
79-
console.log(year);
80-
// Output: 2024
94+
console.log(year);
8195
```
8296

83-
In this example, the output is `2024`, which is the current year. If the year is invalid, it will return `NaN`.
97+
:::
98+
99+
If the year is invalid, it will return `NaN`.
84100

85101
There are many more methods available on the `Date` object including `getHours`, `getMinutes`, `getSeconds`, and so on. I encourage you to explore some more on your own through Mozilla's documentation or other resources.
86102

0 commit comments

Comments
 (0)