You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
11
11
@@ -23,25 +23,31 @@ The `new` keyword is used to create a new instance of the `Date` object, and the
23
23
24
24
Here is an example response you would see if you logged the value of `now` to the console:
25
25
26
+
:::interactive_editor
27
+
26
28
```js
27
29
constnow=newDate();
28
30
console.log(now);
29
-
// Mon Mar 15 2024 14:30:00 GMT-0700 (Pacific Daylight Time)
30
31
```
31
32
33
+
:::
34
+
32
35
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.
33
36
34
37
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.
35
38
36
39
Here is an example of creating a new `Date` object with a specific date and time:
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.
46
52
47
53
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
50
56
51
57
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:
52
58
59
+
:::interactive_editor
60
+
53
61
```js
54
62
constnow=newDate();
55
63
constdate=now.getDate();
56
-
console.log(date); // 15
64
+
console.log(date);
57
65
```
58
66
67
+
:::
68
+
59
69
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.
60
70
61
71
`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).
62
72
63
73
To get the month, you can use the `getMonth` method like this:
64
74
75
+
:::interactive_editor
76
+
65
77
```js
66
78
constnow=newDate();
67
79
constmonth=now.getMonth();
68
80
console.log(month);
69
-
// 2
70
81
```
71
82
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`.
73
86
74
87
If you need to get the full year, then you can use the `getFullYear` method like this:
75
88
89
+
:::interactive_editor
90
+
76
91
```js
77
92
constnow=newDate();
78
93
constyear=now.getFullYear();
79
-
console.log(year);
80
-
// Output: 2024
94
+
console.log(year);
81
95
```
82
96
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`.
84
100
85
101
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.
0 commit comments