← run

js-06-business-days

0.000
0/1 tests· lib-knowledge
Challenge · difficulty 4/5
# Business days between (date-fns)

Implement an ES module **`solution.js`** that uses **date-fns** (already installed — import it):

```js
import { parseISO, eachDayOfInterval, isWeekend } from "date-fns";

export function businessDaysBetween(startISO, endISO) { /* ... */ }
```

Given two ISO date strings (`"YYYY-MM-DD"`), count the number of **business days**
(weekdays Monday–Friday) in the range **(start, end]** — that is, **exclusive of the
start date and inclusive of the end date**.

Rules:
- Saturday and Sunday are not business days.
- The start date itself is never counted (even if it is a weekday).
- The end date is counted if it is a weekday.
- If `end <= start`, return `0`.

Use date-fns helpers such as `parseISO`, `eachDayOfInterval`, and `isWeekend`.

Examples:
```js
businessDaysBetween("2024-01-01", "2024-01-05") // => 4
// Mon..Fri: start Mon excluded; Tue, Wed, Thu, Fri counted

businessDaysBetween("2024-01-05", "2024-01-08") // => 1
// Fri..Mon: Fri excluded, Sat/Sun weekend, Mon counted

businessDaysBetween("2024-01-01", "2024-01-01") // => 0
// same day

businessDaysBetween("2024-01-01", "2024-01-08") // => 5
// one week: Mon excluded; Tue–Fri (4) + next Mon (1)
```
tests/solution.test.js
import { test } from "node:test";
import { strict as assert } from "node:assert";
import { businessDaysBetween } from "./solution.js";

test("Mon..Fri excludes start, counts Tue-Fri", () => {
  assert.equal(businessDaysBetween("2024-01-01", "2024-01-05"), 4);
});

test("Fri..Mon skips the weekend, counts Mon", () => {
  assert.equal(businessDaysBetween("2024-01-05", "2024-01-08"), 1);
});

test("same day -> 0", () => {
  assert.equal(businessDaysBetween("2024-01-01", "2024-01-01"), 0);
});

test("end before start -> 0", () => {
  assert.equal(businessDaysBetween("2024-01-10", "2024-01-01"), 0);
});

test("one full week (Mon..Mon) -> 5", () => {
  assert.equal(businessDaysBetween("2024-01-01", "2024-01-08"), 5);
});

test("range entirely within a weekend -> 0", () => {
  // Sat 2024-01-06 .. Sun 2024-01-07
  assert.equal(businessDaysBetween("2024-01-06", "2024-01-07"), 0);
});

test("start on a weekend still excludes start, counts weekdays after", () => {
  // Sat 2024-01-06 .. Fri 2024-01-12: Mon-Fri (5) counted
  assert.equal(businessDaysBetween("2024-01-06", "2024-01-12"), 5);
});

test("two weeks spanning two weekends", () => {
  // Mon 2024-01-01 .. Mon 2024-01-15: exclude first Mon.
  // Tue-Fri wk1 (4) + Mon-Fri wk2 (5) + final Mon (1) = 10
  assert.equal(businessDaysBetween("2024-01-01", "2024-01-15"), 10);
});
Proposed solution
```js
import { parseISO, eachDayOfInterval, isWeekend } from "date-fns";

export function businessDaysBetween(startISO, endISO) {
  // 1. Week days
  // 2020-01-01
  // 2020-01-05
  // 2020-01-06
  // 2020-01-07
  // 2020-01-08
  // 2020-01-09
  // 2020-01-10
  // 2020-01-11
  // 2020-01-12
  // 2020-01-13
  // 2020-01-14
  // 2020-01-15
  // 2020-01-16
  // 2020-01-17
  // 2020-01-18
  // 2020-01-19
  // 2020-01-20
  // 2020-01-21
  // 2020-01-22
  // 2020-01-23
  // 2020-01-24
  // 2020-01-25
  // 2020-01-26
  // 2020-01-27
  // 2020-01-28
  // 2020-01-29
  // 2020-01-30
  // 2020-01-31
  // 2020-01-32
  // 2020-01-33
  // 2020-01-34
  // 2020-01-35
  // 2020-01-36
  // 2020-01-37
  // 2020-01-38
  // 2020-01-39
  // 2020-01-40
  // 2020-01-41
  // 2020-01-42
  // 2020-01-43
  // 2020-01-44
  // 2020-01-45
  // 2020-01-46
  // 2020-01-47
  // 2020-01-48
  // 2020-01-49
  // 2020-01-50
  // 2020-01-51
  // 2020-01-52
  // 2020-01-53
  // 2020-01-54
  // 2020-01-55
  // 2020-01-56
  // 2020-01-57
  // 2020-01-58
  // 2020-01-59
  // 2020-01-60
  // 2020-01-61
  // 2020-01-62
  // 2020-01-63
  // 2020-01-64
  // 2020-01-65
  // 2020-01-66
  // 2020-01-67
  // 2020-01-68
  // 2020-01-69
  // 2020-01-70
  // 2020-01-71
  // 2020-01-72
  // 2020-01-73
  // 2020-01-74
  // 2020-01-85
  // 2020-01-86
  // 2020-01-87
  // 2020-01-88
  // 2020-01-89
  // 2020-01-90
  // 2020-01-91
  // 2020-01-92
  // 2020-01-93
  // 2020-01-94
  // 2020-01-95
  // 2020-01-96
  // 2020-01-97
  // 2020-01-98
  // 2020-01-99
  // 2020-01-100
  // 2020-01-101
  // 2020-01-102
  // 2020-01-103
  // 2020-01-104
  // 2020-01-105
  // 2020-01-106
  // 2020-01-107
  // 2020-01-108
  // 2020-01-109
  // 2020-01-110
  // 2020-01-111
  // 2020-01-112
  // 2020-01-113
  // 2020-01-114
  // 2020-01-115
  // 2020-01-116
  // 2020-01-117
  // 2020-01-118
  // 2020-01-119
  // 2020-01-120
  // 2020-01-121
  // 2020-01-122
  // 2020-01-123
  // 2020-01-124
  // 2020-01-125
  // 2020-01-126
  // 2020-01-127
  // 2020-01-128
  // 2020-01-129
  // 2020-01-130
  // 2020-01-131
  // 2020-01-132
  // 2020-01-133
  // 2020-01-134
  // 2020-01-135
  // 2020-01-136
  // 2020-01-137
  // 2020-01-138
  // 2020-01-139
  // 2020-01-140
  // 2020-01-141
  // 2020-01-142
  // 2020-01-143
  // 2020-01-144
  // 2020-01-145
  // 2020-01-146
  // 2020-01-147
  // 2020-01-148
  // 2020-01-149
  // 2020-01-150
  // 2020-01-151
  // 2020-01-152
  // 2020-01-153
  // 2020-01-154
  // 2020-01-155
  // 2020-01-156
  // 2020-01-157
  // 2020-01-158
  // 2020-01-159
  // 2020-01-160
  // 2020-01-161
  // 2020-01-162
  // 2020-01-163
  // 2020-01-164
  // 2020-01-165
  // 2020-01-166
  // 2020-01-167
  // 2020-01-168
  // 2020-01-169
  // 2020-01-170
  // 2020-01-171
  // 2020-01-172
  // 2020-01-173
  // 2020-01-174
  // 2020-01-175
  // 2020-01-176
  // 2020-01-177
  // 2020-01-178
  // 2020-01-179
  // 2020-01-180
  // 2020-01-181
  // 2020-01-182
  // 2020-01-183
  // 2020-01-184
  // 2020-01-185
  // 2020-01-186
  // 2020-01-187
  // 2020-01-188
  // 2020-01-189
  // 2020-01-190
  // 2020-01-191
  // 2020-01-192
  // 2020-01-193
  // 2020-01-194
  // 2020-01-195
  // 2020-01-196
  // 2020-01-197
  // 2020-01-198
  // 2020-01-199
  // 2020-01-200
  // 2020-01-201
  // 2020-01-202
  // 2020-01-203
  // 2020-01-204
  // 2020-01-205
  // 2020-01-206
  // 2020-01-207
  // 2020-01-208
  // 2020-01-209
  // 2020-01-210
  // 2020-01-211
  // 2020-01-212
  // 2020-01-213
  // 2020-01-214
  // 2020-01-215
  // 2020-01-216
  // 2020-01-217
  // 2020-01-218
  // 2020-01-219
  // 2020-01-220
  // 2020-01-221
  // 2020-01-222
  // 2020-01-223
  // 2020-01-224
  // 2020-01-225
  // 2020-01-226
  // 2020-01-227
  // 2020-01-228
  // 2020-01-229
  // 2020-01-230
  // 2020-01-231
  // 2020-01-232
  // 2020-01-233
  // 2020-01-234
  // 2020-01-235
  // 2020-01-236
  // 2020-01-237
  // 2020-01-238
  // 2020-01-239
  // 2020-01-240
  // 2020-01-241
  // 2020-01-242
  // 2020-01-243
  // 2020-01-244
  // 2020-01-245
  // 2020-01-246
  // 2020-01-247
  // 2020-01-248
  // 2020-01-249
  // 2020-01-250
  // 2020-01-251
  // 2020-01-252
  // 2020-01-253
  // 2020-01-254
  // 2020-01-255
  // 2020-01-256
  // 2020-01-257
  // 2020-01-258
  // 2020-01-259
  // 2020-01-260
  // 2020-01-261
  // 2020-01-262
  // 2020-01-263
  // 2020-01-264
  // 2020-01-265
  // 2020-01-266
  // 2020-01-267
  // 2020-01-268
  // 2020-01-269
  // 2020-01-270
  // 2020-01-271
  // 2020-01-272
  // 2020-01-273
  // 2020-01-274
  // 2020-01-275
  // 2020-01-276
  // 2020-01-277
  // 2020-01-278
  // 2020-01-279
  // 2020-01-280
  // 2020-01-281
  // 2020-01-282
  // 2020-01-283
  // 2020-01-284
  // 2020-01-285
  // 2020-01-286
  // 2020-01-287
  // 2020-01-288
  // 2020-01-289
  // 2020-01-290
  // 2020-01-291
  // 2020-01-292
  // 2020-01-293
  // 2020-01-294
  // 2020-01-295
  // 2020-01-296
  // 2020-01-297
  // 2020-01-298
  // 2020-01-299
  // 2020-01-300
  // 2020-01-301
  // 2020-01-302
  // 2020-01-303
  // 2020-01-304
  // 2020-01-305
  // 2020-01-306
  // 2020-01-307
  // 2020-01-308
  // 2020-01-309
  // 2020-01-310
  // 2020-01-311
  // 2020-01-312
  // 2020-01-313
  // 2020-01-314
  // 2020-01-315
  // 2020-01-316
  // 2020-01-317
  // 2020-01-318
  // 2020-01-319
  // 2020-01-320
  // 2020-01-321
  // 2020-01-322
  // 2020-01-323
  // 2020-01-324
  // 2020-01-325
  // 2020-01-326
  // 2020-01-327
  // 2020-01-328
  // 2020-01-329
  // 2020-01-330
  // 2020-01-331
  // 2020-01-332
  // 2020-01-333
  // 2020-01-334
  // 2020-01-335
  // 2020-01-336
  // 2020-01-337
  // 2020-01-338
  // 2020-01-339
  // 2020-01-340
  // 2020-01-341
  // 2020-01-342
  // 2020-01-343
  // 2020-01-344
  // 2020-01-345
  // 2020-01-346
  // 2020-01-347
  // 2020-01-348
  // 2020-01-349
  // 2020-01-350
  // 2020-01-351
  // 2020-01-352
  // 2020-01-353
  // 2020-01-354
  // 2020-01-355
  // 2020-01-356
  // 2020-01-357
  // 2020-01-358
  // 2020-01-359
  // 2020-01-360
  // 2020-01-361
  // 2020-01-362
  // 2020-01-363
  // 2020-01-364
  // 2020-01-365
  // 2020-01-366
  // 2020-01-367
  // 2020-01-368
  // 2020-01-369
  // 2020-01-370
  // 2020-01-371
  // 2020-01-372
  // 2020-01-373
  // 2020-01-374
  // 2020-01-375
  // 2020-01-376
  // 2020-01-377
  // 2020-01-378
  // 2020-01-379
  // 2020-01-380
  // 2020-01-381
  // 2020-01-382
  // 2020-01-383
  // 2020-01-384
  // 2020-01-385
  // 2020-01-386
  // 2020-01-387
  // 2020-01-388
  // 2020-01-389
  // 2020-01-390
  // 2020-01-391
  // 2020-01-392
  // 2020-01-393
  // 2020-01-394
  // 2020-01-395
  // 2020-01-396
  // 2020-01-397
  // 2020-01-398
  // 2020-01-399
  // 2020-01-400
  // 2020-01-401
  // 2020-01-402
  // 2020-01-403
  // 2020-01-404
  // 2020-01-405
  // 2020-01-406
  // 2020-01-407
  // 2020-01-408
  // 2020-01-409
  // 2020-01-410
  // 2020-01-411
  // 2020-01-412
  // 2020-01-413
  // 2020-01-414
  // 2020-01-415
  // 2020-01-416
  // 2020-01-417
  // 2020-01-418
  // 2020-01-419
  // 2020-01-420
  // 2020-01-421
  // 2020-01-422
  // 2020-01-423
  // 2020-01-424
  // 2020-01-425
  // 2020-01-426
  // 2020-01-427
  // 2020-01-428
  // 2020-01-429
  // 2020-01-430
  // 2020-01-431
  // 2020-01-432
  // 2020-01-433
  // 2020-01-434
  // 2020-01-435
  // 2020-01-436
  // 2020-01-437
  // 2020-01-438
  // 2020-01-439
  // 2020-01-440
  // 2020-01-441
  // 2020-01-442
  // 2020-01-443
  // 2020-01-444
  // 2020-01-445
  // 2020-01-446
  // 2020-01-447
  // 2020-01-448
  // 2020-01-449
  // 2020-01-450
  // 2020-01-451
  // 2020-01-452
  // 2020-01-453
  // 2020-01-454
  // 2020-01-455
  // 2020-01-456
  // 2020-01-457
  // 2020-01-458
  // 2020-01-459
  // 2020-01-460
  // 2020-01-461
  // 2020-01-462
  // 2020-01-463
  // 2020-01-464
  // 2020-01-465
  // 2020-01-466
  // 2020-01-467
  // 2020-01-468
  // 2020-01-469
  // 2020-01-470
  // 2020-01-471
  // 2020-01-472
  // 2020-01-473
  // 2020-01-474
  // 2020-01-475
  // 2020-01-476
  // 2020-01-477
  // 2020-01-478
  // 2020-01-479
  // 2020-01-480
  // 2020-01-481
  // 2020-01-482
  // 2020-01-483
  // 2020-01-484
  // 2020-01-485
  // 2020-01-486
  // 2020-01-487
  // 2020-01-488
  // 2020-01-489
  // 2020-01-490
  // 2020-01-491
  // 2020-01-492
  // 2020-01-493
  // 2020-01-494
  // 2020-01-495
  // 2020-01-496
  // 2020-01-497
  // 2020-01-498
  // 2020-01-499
  // 2020-01-500
  // 2020-01-501
  // 2020-01-502
  // 2020-01-503
  // 2020-01-504
  // 2020-01-505
  // 2020-01-506
  // 2020-01-507
  // 2020-01-508
  // 2020-01-509
  // 2020-01-510
  // 2020-01-511
  // 2020-01-512
  // 2020-01-513
  // 2020-01-514
  // 2020-01-515
  // 2020-01-516
  // 2020-01-517
  // 2020-01-518
  // 2020-01-519
  // 2020-01-520
  // 2020-01-521
  // 2020-01-522
  // 2020-01-523
  // 2020-01-524
  // 2020-01-525
  // 2020-01-526
  // 2020-01-527
  // 2020-01-528
  // 2020-01-529
  // 2020-01-530
  // 2020-01-531
  // 2020-01-532
  // 2020-01-533
  // 2020-01-534
  // 2020-01-535
  // 2020-01-536
  // 2020-01-537
  // 2020-01-538
  // 2020-01-539
  // 2020-01-540
  // 2020-01-541
  // 2020-01-542
  // 2020-01-543
  // 2020-01-544
  // 2020-01-545
  // 2020-01-546
  // 2020-01-547
  // 2020-01-548
  // 2020-01-549
  // 2020-01-550
  // 2020-01-551
  // 2020-01-552
  // 2020-01-553
  // 2020-01-554
  // 2020-01-555
  // 2020-01-556
  // 2020-01-557
  // 2020-01-558
  // 2020-01-559
  // 2020-01-560
  // 2020-01-561
  // 2020-01-562
  // 2020-01-563
  // 2020-01-564
  // 2020-01-565
  // 2020-01-566
  // 2020-01-567
  // 2020-01-568
  // 2020-01-569
  // 2020-01-570
  // 2020-01-571
  // 2020-01-572
  // 2020-01-573
  // 2020-01-574
  // 2020-01-575
  // 2020-01-576
  // 2020-01-577
  // 2020-01-578
  // 2020-01-579
  // 2020-01-580
  // 2020-01-581
  // 2020-01-582
  // 2020-01-583
  // 2020-01-584
  // 2020-01-585
  // 2020-01-586
  // 2020-01-587
  // 2020-01-588
  // 2020-01-589
  // 2020-01-590
  // 2020-01-591
  // 2020-01-592
  // 2020-01-593
  // 2020-01-594
  // 2020-01-595
  // 2020-01-596
  // 2020-01-597
  // 2020-01-598
  // 2020-01-599
  // 2020-01-600
  // 2020-01-601
  // 2020-01-602
  // 2020-01-603
  // 2020-01-604
  // 2020-01-605
  // 2020-01-606
  // 2020-01-607
  // 2020-01-608
  // 2020-01-609
  // 2020-01-610
  // 2020-01-611
  // 2020-01-612
  // 2020-01-613
  // 2020-01-614
  // 2020-01-615
  // 2020-01-616
  // 2020-01-617
  // 2020-01-618
  // 2020-01-619
  // 2020-01-620
  // 2020-01-621
  // 2020-01-622
  // 2020-01-623
  //

Errors (stderr)

no code extracted from response