Skip to content

Commit 6656525

Browse files
committed
Add exercise
1 parent 7ee315a commit 6656525

File tree

15 files changed

+734
-0
lines changed

15 files changed

+734
-0
lines changed

config.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,21 @@
404404
"template-strings"
405405
],
406406
"status": "beta"
407+
},
408+
{
409+
"slug": "appointment-time",
410+
"name": "Appointment Time",
411+
"uuid": "0f694053-a388-457f-89ca-f49be4560469",
412+
"concepts": [
413+
"dates"
414+
],
415+
"prerequisites": [
416+
"classes",
417+
"objects",
418+
"conditionals",
419+
"type-conversion"
420+
],
421+
"status": "beta"
407422
}
408423
],
409424
"practice": [
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Hints
2+
3+
## 1. Create an appointment
4+
5+
- You need to create a new date. The introduction elaborates on the different ways.
6+
- `Date.now()` gives you current time in milliseconds
7+
- A day consist of 24 hour. An hour consist of 60 minutes. A minute consist of 60 seconds. A second consist of 1000 milliseconds. In order to get timestamp of `n` days later from current date, you can sum current timestamp and `n * 24 * 60 * 60 * 1000`.
8+
9+
## 2. Convert a date into a timestamp
10+
11+
- The introduction lists the various ways how to convert a date to different types. Can you use one of those methods?
12+
13+
## 3. Get the details of an appointment
14+
15+
- The introduction has all the required information to extract the information from a date.
16+
17+
## 4. Update an appointment with the given options
18+
19+
- The introduction has all the required information to extract the information from a date.
20+
- You can reuse `getAppointmentDetails`
21+
22+
## 5. Get available times between two appointments
23+
24+
- General subtraction between two dates will give you the timestamp between the two dates.
25+
- You probably want to convert to a number first, and not rely on type-coercion.
26+
27+
## 6. Check if an appointment is now valid or not
28+
29+
- Conditional operators will help you to decide which date is bigger or smaller between two dates.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Instructions
2+
3+
In this exercise you will work on some functions in order to manage appointments.
4+
The system stores everything in ISO 8601 formatted strings, but that's not how people use the calendar.
5+
Various functions are necessary to convert between the various formats.
6+
7+
## 1. Create an appointment
8+
9+
Create an appointment `n` days from now at current time.
10+
The function takes `n` days and return the appointment time of `n` days from now.
11+
12+
```javascript
13+
createAppointment(4, now);
14+
// Given now is Sun Oct 05 2022 23:28:43 GMT+0600 (Bangladesh Standard Time)
15+
// => Sun Oct 09 2022 23:28:43 GMT+0600 (Bangladesh Standard Time)
16+
```
17+
18+
If the second parameter `now` is unused, the current time should be used instead.
19+
20+
## 2. Convert a date into a timestamp
21+
22+
Various tools only work with the internationally standardized ISO 8601 format.
23+
Write the function `getAppointmentTimestamp` to take a date and return a string in that format.
24+
25+
```javascript
26+
const appointment = new Date(Date.UTC(2010, 6, 16, 12, 42, 0, 0));
27+
28+
getAppointmentTimestamp(appointment);
29+
// => '2010-07-16T12:42:00.000Z'
30+
```
31+
32+
## 3. Get the details of an appointment
33+
34+
Timestamps are hard to read; a function to get the appointment details should help with that.
35+
The function `getAppointmentDetails` takes a timestamp in the ISO 8601 format, and returns the year, month, date, hour, and minute.
36+
37+
```javascript
38+
getAppointmentDetails('2022-04-24T08:15:00.000');
39+
// => { year: 2022, month: 3, date: 24, hour: 8, minute: 15 }
40+
```
41+
42+
## 4. Update an appointment with the given options
43+
44+
The function will receive first argument as appointment time and second argument of object of some options.
45+
You have to update the appointment according to the options in the object and return the new appointment date.
46+
The options object could have multiple options.
47+
48+
```javascript
49+
updateAppointment('2022-02-09T09:20:00.000', { month: 6 });
50+
// => { year: 2022, month: 6, date: 9, hour: 10, minute: 20 }
51+
```
52+
53+
## 5. Get the available time between two appointments
54+
55+
The function will receive two appointments (timestamps) as arguments.
56+
You have to return the difference between those two times in seconds.
57+
58+
Because half a second is almost meaningless, round the number before returning it.
59+
60+
```javascript
61+
timeBetween('2022-12-12T09:20:00.000', '2022-12-18T08:30:00.000');
62+
// => 515400
63+
```
64+
65+
## 6. Check if an appointment is now valid or not
66+
67+
Finally, when the appointment is made, the system needs to check if it's valid.
68+
In other words, the appointment must be in the future, and not the past.
69+
70+
Write the function `isValid` which takes two arguments, an appointment timestamp (string), and the current time as a timestamp (string) and returns `true` if the appointment is in the future, given the current time.
71+
72+
```javascript
73+
isValid('2022-02-11T23:00:00.000', '2022-02-08T23:00:00.000');
74+
// => true
75+
```
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
# Introduction
2+
3+
JavaScript has a built-in object `Date` which stores date and time, and provides methods for their management.
4+
5+
<!-- prettier-ignore -->
6+
~~~exercism/caution
7+
It was based on Java's `java.util.Date` class, which was replaced in the early 2010s, but for backwards compatibility, JavaScript's `Date` sticks around.
8+
9+
Because of how hard it is to work with Dates in general and because of how bad or non-existing timezone handling is, many libraries exist such as `moment.js`, `day.js`, `date-fns` and `luxon`.
10+
None of these are available on Exercism.
11+
12+
In your own projects, do not use a deprecated / unmaintained package such as `moment.js` but rely on more modern alternatives like `luxon`, or the not yet widely available [Temporal][mdn-temporal].
13+
This exercise focusses on `Date`, which will remain relevant until the end of JavaScript.
14+
~~~
15+
16+
## Creation
17+
18+
A `Date` object in an instance of the `Date` class.
19+
It can be created without passing any arguments to the constructor function.
20+
This results in a `Date` object that represents the current date and time:
21+
22+
```javascript
23+
const now = new Date();
24+
// => Thu Apr 14 2022 11:46:08 GMT+0530 (India Standard Time)
25+
// Shows current day, date and time (in your time zone).
26+
```
27+
28+
### Unix timestamp (number)
29+
30+
If a number is passed in, this will be interpreted as a `timestamp`.
31+
A timestamp is an integer number representing the number of **milliseconds** that has passed since **1 January 1970 [UTC][defn-utc]+0**.
32+
33+
```javascript
34+
const epoch = new Date(0);
35+
// Thu Jan 01 1970 01:00:00 GMT+0100 (Central European Standard Time)
36+
37+
const another = new Date(1749508766627);
38+
// Tue Jun 10 2025 00:39:26 GMT+0200 (Central European Summer Time)
39+
```
40+
41+
One may expect `new Date(0)` to generate the "earliest" date object, but JavaScript will convert the date to your local timezone, which means that only those around [GMT / with an UTC+0][defn-gmt] timezone will actually get the [Unix epoch][defn-unix-epoch] value.
42+
43+
### ISO 8601 timestamp (string)
44+
45+
You can pass a string value representing a date to the `Date` constructor.
46+
The **only** format that is consistent across implementations is the [simplified version][mdn-date-string-format] of the internationally recognized and standardized so-called [ISO 8601 timestamp strings][defn-iso8601].
47+
48+
A moment in time at [UTC][defn-gmt] looks like this:
49+
50+
```text
51+
YYYY-MM-DDTHH:MM:SSZ
52+
YYYYMMDDTHHMMSSZ
53+
```
54+
55+
Where the following substitutions take place:
56+
57+
| Key | Description |
58+
| ---- | ------------------------------------------- |
59+
| YYYY | The calendar year, represented in 4 digits |
60+
| MM | The calendar month, represented in 2 digits |
61+
| DD | The calendar day, represented in 2 digits |
62+
| HH | The hours in a 24-hour clock, 2 digits |
63+
| MM | The minutes, 2 digits |
64+
| SS | The seconds, 2 digits |
65+
66+
The letter `T` separates the date from the time.
67+
The letter `Z` indicates UTC (no timezone, no Day Light Savings).
68+
69+
<!-- prettier-ignore -->
70+
~~~exercism/caution
71+
Other formats that are accepted by `Date.parse` may or may not work.
72+
When working with Dates in JavaScript, _always_ use an ISO 8601 timestamp when converting from a `string` to a `Date`.
73+
74+
Date-only forms are allowed, but not all ISO 8601 formats are supported.
75+
Consult the [simplified version explanation page on MDN][mdn-date-string-format].
76+
~~~
77+
78+
If the timestamp does not end in `Z`, and it does not end with `+HH:MM` or `-HH:MM`, indicating a timezone offset, because of historical reasons, the following applies:
79+
80+
> When the time zone offset is absent, date-only forms are interpreted as a UTC time and date-time forms are interpreted as a local time.
81+
> The interpretation as a UTC time is due to a historical spec error that was not consistent with ISO 8601 but could not be changed due to web compatibility.
82+
> See [Broken Parser – A Web Reality Issue][ref-broken-parser].
83+
84+
### Date object
85+
86+
An existing date object can also be used as a constructor argument.
87+
This makes a copy of the existing `Date` object with the same date and time.
88+
89+
```javascript
90+
const t1 = new Date();
91+
const t2 = new Date(t1);
92+
// Values of t1 and t2 will be the same.
93+
```
94+
95+
### Supplying individual date and time component values
96+
97+
A date representing a date can be created by passing three numbers.
98+
A date representing a date and time can be created by passing in 6 numbers.
99+
100+
```javascript
101+
const date1 = new Date(95, 11, 17);
102+
// Creates Date for Dec 17 1995 00:00 if your local timezone is equivalent to UTC.
103+
104+
const date2 = new Date(2013, 12, 5, 13, 24, 0);
105+
// Creates Date for Jan 5 2014 13:24 if your local timezone is equivalent to UTC.
106+
```
107+
108+
The second value is the `month`, which starts at `0` for January, up to `11` for December.
109+
110+
## `Date.parse()`
111+
112+
You may find mentions of or references to a date parsing function `Date.parse`.
113+
Because there are only a few invariants (truths) for this function and because the rest of the implementation is not specified (and thus not standard), one should not use it.
114+
115+
## Accessing `Date` components
116+
117+
There are various methods on date objects that return the components of the date:
118+
119+
```javascript
120+
getFullYear(); // Get the year (4 digits)
121+
getMonth(); // Get the month, from 0 to 11.
122+
getDate(); // Get the day of month, from 1 to 31.
123+
getHours(); // Get the hour in a 24 clock, from 0 to 23
124+
getMinutes(); // Get the minutes, from 0 to 59
125+
getSeconds(); // Get the seconds, from 0 to 59
126+
getMilliseconds(); // Get the milliseconds, from 0 and 999
127+
getDay(); // Get the day of week, from 0 (Sunday) to 6 (Saturday).
128+
```
129+
130+
Each of these has an applicable `set` variant (e.g. `setFullYear`) to update the value.
131+
Any overflowing value rolls over to the next component:
132+
133+
```javascript
134+
const date = new Date('2025-02-28T12:42:00Z');
135+
// => Fri Feb 28 2025 13:42:00 GMT+0100 (Central European Standard Time)
136+
137+
date.setDate(29);
138+
// there was no February 29th in 2025.
139+
140+
date.getDate();
141+
// => 1
142+
143+
date.toString();
144+
// => Sat Mar 01 2025 13:42:00 GMT+0100 (Central European Standard Time)
145+
```
146+
147+
There are UTC variants for all the methods that disregard the local timezone.
148+
149+
## Converting from date
150+
151+
Date objects have a method `getTime()` that returns the UNIX timestamp in milliseconds, ie. amount of milliseconds the midnight at the beginning of January 1, 1970, UTC.
152+
Additionally, a method `toISOString()` is available to convert from a date object to a ISO 8601 timestamp string.
153+
154+
## Comparing Dates
155+
156+
Greater than (`>`) and greater than or equals (`>=`) as well as less than (`<`) and less than or equals (`<=`) can be used directly between two dates or a date and a number.
157+
This works because JavaScript will try to coerce the date to a primitive.
158+
159+
<!-- prettier-ignore -->
160+
~~~@exercism/advanced
161+
When doing a comparison between two dates or date and a number, JavaScript calls [`[Symbol.toPrimitive]("number")`][mdn-to-primitive] which internally calls [`date.valueOf()`][mdn-date-value-of].
162+
The latter is the same as calling [`date.getTime()`][mdn-date-get-time].
163+
164+
If you do not want to rely on this behaviour, convert to a number using `getTime()` first.
165+
~~~
166+
167+
Dates cannot be compared using equality (`==`, and `===`), but the result of `.getTime()` can.
168+
169+
[defn-utc]: https://simple.wikipedia.org/wiki/Coordinated_Universal_Time
170+
[defn-gmt]: https://simple.wikipedia.org/wiki/Greenwich_Mean_Time
171+
[defn-unix-epoch]: https://en.wikipedia.org/wiki/Epoch_%28computing%29
172+
[defn-iso8601]: https://en.wikipedia.org/wiki/ISO_8601
173+
[mdn-temporal]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal
174+
[mdn-diff-assumed-timezone]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse#differences_in_assumed_time_zone
175+
[mdn-date-string-format]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#date_time_string_format
176+
[mdn-to-primitive]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Symbol.toPrimitive
177+
[mdn-date-value-of]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/valueOf
178+
[mdn-date-get-time]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime
179+
[ref-broken-parser]: https://maggiepint.com/2017/04/11/fixing-javascript-date-web-compatibility-and-reality/
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/node_modules
2+
/bin/configlet
3+
/bin/configlet.exe
4+
/package-lock.json
5+
/yarn.lock
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"authors": [
3+
"SalahuddinAhammed",
4+
"SleeplessByte"
5+
],
6+
"files": {
7+
"solution": [
8+
"appointment-time.js"
9+
],
10+
"test": [
11+
"appointment-time.spec.js"
12+
],
13+
"exemplar": [
14+
".meta/exemplar.js"
15+
]
16+
},
17+
"blurb": "Learn how to work with dates by managing appointments",
18+
"custom": {
19+
"version.tests.compatibility": "jest-29",
20+
"flag.tests.task-per-describe": true,
21+
"flag.tests.may-run-long": false,
22+
"flag.tests.includes-optional": false
23+
}
24+
}

0 commit comments

Comments
 (0)