See why a typed word is not a date and where the built-in Date trips you up, install date-fns, turn 2026-10-30 into a real date with parseISO, refuse one that is not a date with isValid, print it with format, and count the days left in calendar days, so TaskKit’s list says due: Fri 30 Oct, in 3 days.
TypeScript
Fundamentals
`date-fns`: working with due dates. Read this lesson at .
Tested with TypeScript 7.x · Node.js LTS
On Tuesday you told TaskKit that the rent is due on Friday, and the
list has said so ever since:
Outputtext
#1 [todo] Pay rent, due: friday
On Saturday morning it still says due: friday. Which Friday? The
one that was yesterday, or the next one? TaskKit cannot tell you that
the rent is late, because to TaskKit friday is six letters, not a
day. It is a sticky note with "Friday" on it: fine for the week you
wrote it, useless the week after.
A calendar does better. The day is circled, so anyone can count how
far away it is, or how long ago it was. In this lesson TaskKit gets a
calendar of its own, through a package called date-fns. By the end,
--due2026-10-30 is a real date, --due friday is refused before
it is saved, and the list prints due: Fri 30 Oct, in 3 days, or
2 days late once the day is gone.
A date is not a word
JavaScript has dates built in. A Date is an object that holds
one moment in time, to the millisecond, and you make one with new,
as you made a TaskStore (lesson 13.1). Given numbers, new Date
takes the year, the month and the day, in that order. Run this, which
should give 30 October 2026:
dates.tsts
const due = new Date(2026, 10, 30);console.log(due.toDateString());console.log(new Date("friday").toDateString());
toDateString is a method every Date has: it writes the date as
text, weekday first. The first line prints Mon Nov 30 2026, a month
late. The built-in Date counts months from 0, as an array counts its
indexes (lesson 4.1): January is 0, so 10 is November and October is
9. The second line prints Invalid Date. new Date could
find no day in friday, and instead of stopping it gives you an
Invalid Date: an object that looks like a date but holds no
moment at all.
Those are two of the built-in Date's traps, and they are not the
only ones. It also has no ready-made way to say how many days lie
between two dates, and only a clumsy one to print a date the way you
want it, such as Fri 30 Oct. date-fns fills that gap. The name
means "date functions": it is a few hundred small functions, each
doing one job with a Date. You give one a date, and it gives you
back an answer or a new date; it never changes the date you gave it.
Install date-fns
Lesson 14.1 already put date-fns through its four questions: it does
the job, it had tens of millions of downloads a week when that lesson
looked, it is looked after, and its types are built in. In the
taskkit folder, run:
bash
npm install date-fns
Outputtext
added 1 package, and audited 23 packages in 919ms15 packages are looking for funding run `npm fund` for detailsfound 0 vulnerabilities
added 1 package is date-fns alone: like commander, it needs no
packages of its own. package.json gains "date-fns": "^4.4.0", and
the lock gains one entry. Your time, in milliseconds (ms), will
differ, and so may the version if a newer date-fns 4 is out.
From text to a date
Whatever you type after --due reaches TaskKit as a string. Before
TaskKit can count days with it, the text has to become a Date, and
something has to make sure there is a real day in it. Then the date
is printed for people, and the days left are counted. Switch between
the three dates, and press Next step to walk each one through, or
let it play:
Typed: reached--due gives TaskKit a string
parseISO: reachedTurns the text into a Date
isValid: passes✓Is there a real day in it?
format: reachedFri 30 Oct, for people
Days left: reachedin 3 days, counted from today
The text you type becomes a Date, is checked, and only then
is printed for people
All 6 steps shown.Step 1 of 6: TypedStep 2 of 6: parseISOStep 3 of 6: isValidStep 4 of 6: isValid: passesStep 5 of 6: formatStep 6 of 6: Days left
All 6 steps shown.
Typed: reached--due gives TaskKit a string
parseISO: reachedTurns the text into a Date
isValid: stops here✕Is there a real day in it?
format: never reachedFri 30 Oct, for people
Days left: never reachedin 3 days, counted from today
isValid stops hereNot a date: refused, nothing saved
The text you type becomes a Date, is checked, and only then
is printed for people
All 4 steps shown.Step 1 of 4: TypedStep 2 of 4: parseISOStep 3 of 4: isValidStep 4 of 4: isValid: stops here. Not a date: refused, nothing saved
All 4 steps shown.
Typed: reached--due gives TaskKit a string
parseISO: reachedTurns the text into a Date
isValid: stops here✕Is there a real day in it?
format: never reachedFri 30 Oct, for people
Days left: never reachedin 3 days, counted from today
isValid stops hereNot a date: refused, nothing saved
The text you type becomes a Date, is checked, and only then
is printed for people
All 4 steps shown.Step 1 of 4: TypedStep 2 of 4: parseISOStep 3 of 4: isValidStep 4 of 4: isValid: stops here. Not a date: refused, nothing saved
All 4 steps shown.
The two stages after Typed are parseISO and isValid. parseISO
reads a date written year, month, day, with dashes: 2026-10-30.
That order is the international standard, called ISO 8601 after
ISO, the International Organization for Standardization, which writes
such standards; the name parseISO is parse (read text into a
value) plus ISO. TaskKit
uses it for two reasons. It means the same in every country, while
03/04 is 3 April in one country and 4 March in another. And the
month is written as people write it: 10 is October.
isValid answers one question about a Date: does it hold a
real moment? It gives true or false. To try both, save this as
try.ts in taskkit and run it with node try.ts:
try.tsts
import { isValid, parseISO } from "date-fns";const due = parseISO("2026-10-30");console.log(due.toDateString());console.log(isValid(due));console.log(isValid(parseISO("friday")));console.log(isValid(parseISO("2026-02-30")));
What Node printstext
Fri Oct 30 2026truefalsefalse
friday has no year, month and day in it, and February has no 30th,
so both give an Invalid Date, and isValid says false. parseISO
does not stop your program on bad text: it gives back an Invalid Date
and carries on. Only later, when format gets that date, does
date-fns stop (the common mistake below). Checking is your job, and
isValid is how you do it.
Print it for people
2026-10-30 is good for typing and saving, but a list reads better
with Fri 30 Oct. format takes a date and a pattern, and gives
back the date as text. Every letter in the pattern stands for a part
of the date; anything else, such as a space or a dot, is copied as it
is:
Letters
What you get
For 30 October 2026
EEE / EEEE
Weekday, short / long
Fri / Friday
d / dd
Day; dd writes 5 as 05
30 / 30
MMM / MMMM
Month, short / long
Oct / October
MM
Month, as two digits
10
yyyy
Year
2026
Put this in try.ts, in place of what is there, and run node try.ts:
try.tsts
import { format, parseISO } from "date-fns";const due = parseISO("2026-10-30");console.log(format(due, "EEE d MMM"));console.log(format(due, "EEEE d MMMM yyyy"));console.log(format(due, "dd.MM.yyyy"));
What Node printstext
Fri 30 OctFriday 30 October 202630.10.2026
Upper and lower case are different letters to format, and that is
the easiest place to slip.
To try the pair below, add its Bad line at the end of try.ts and run
node try.ts: the new line prints last. Then swap it for the Good line
and run it again.
Bad — mm is minutes: parseISO
gives 00:00, midnight, so mm prints its minutes.
try.tsts
console.log(format(due, "dd.mm"));
What Node printstext
30.00
Good — MM is the month.
try.tsts
console.log(format(due, "dd.MM"));
What Node printstext
30.10
Nothing warns you about the first one: both are patterns format
knows, and 30.00 looks enough like a date to slip into a list
unnoticed. When a date prints wrong, look at the case of each letter
first.
How many days are left
The last stage of the figure above, Days left, counts the days between
today and the due date. date-fns has two functions for it, and on a
Tuesday evening they disagree. Put this in try.ts in place of what
is there, and run it. new Date also takes an hour and a minute after
the day, so tuesdayEvening is 27 October at 18:00 (month 9, from 0):
try.tsts
import { differenceInCalendarDays, differenceInDays, parseISO,} from "date-fns";const due = parseISO("2026-10-30");const tuesdayEvening = new Date(2026, 9, 27, 18, 0);console.log(differenceInDays(due, tuesdayEvening));console.log(differenceInCalendarDays(due, tuesdayEvening));
What Node printstext
23
Each takes the later date first and the earlier one second. The
difference is in what they count. Switch between the two ways of
counting, and press Next step to walk through the hours, or let
it play:
Days
Counted
Days: TueTueTue
Counted: 1: 24 hours1:24hours1:24hours
Days: WednesdayWednesdayWednesday
Counted: 2: 24 hours2:24hours2:24hours
Days: ThursdayThursdayThursday
Counted: 6 hours6hours6hours
Days: FriFriFri
Time
Tuesday, 18:00: you add a task due on Friday.
differenceInDays counts the first 24 hours: Tuesday 18:00 to
Wednesday 18:00.
Wednesday, the whole day.
The second 24 hours end on Thursday at 18:00.
Thursday, the whole day.
Six hours are left before Friday, not a whole day, so they do
not count. The answer is 2.
Friday, the day the task is due. It begins at 00:00, midnight.
From Tuesday 18:00 to Friday: 2 full days, but 3 calendar
days
All 7 steps shown.Step 1 of 7: Days: TueStep 2 of 7: Counted: 1: 24 hoursStep 3 of 7: Days: WednesdayStep 4 of 7: Counted: 2: 24 hoursStep 5 of 7: Days: ThursdayStep 6 of 7: Counted: 6 hoursStep 7 of 7: Days: Fri
All 7 steps shown.
Days
Counted
Days: TueTueTue
Days: WednesdayWednesdayWednesday
Counted: 111
Days: ThursdayThursdayThursday
Counted: 222
Days: FriFriFri
Counted: 333
Time
Tuesday, 18:00: you add a task due on Friday.
Wednesday, the whole day.
differenceInCalendarDays counts each new date after Tuesday:
Wednesday is 1.
Thursday, the whole day.
Thursday is 2.
Friday, the day the task is due. It begins at 00:00, midnight.
Friday is 3: the task is due in 3 days, as you would say it.
From Tuesday 18:00 to Friday: 2 full days, but 3 calendar
days
All 7 steps shown.Step 1 of 7: Days: TueStep 2 of 7: Days: WednesdayStep 3 of 7: Counted: 1Step 4 of 7: Days: ThursdayStep 5 of 7: Counted: 2Step 6 of 7: Days: FriStep 7 of 7: Counted: 3
All 7 steps shown.
differenceInDays counts whole 24 hours, and drops what is left.
differenceInCalendarDays ignores the clock and counts calendar
days: how many times the calendar turns a page between the two. When
you say "the rent is due in 3 days" on a Tuesday, you mean calendar
days, so that is the one TaskKit uses. It gives a negative number when
the due date is already behind you: a task due last Sunday is -2 on
Tuesday.
TaskKit gets due dates
date-fns is installed, and you have met the four functions TaskKit
needs. Now they go in. Delete try.ts first.
1
Check the date in add
At the top of index.ts, import the two functions that read and
check a date:
index.tsts
import { Command } from "commander";import { isValid, parseISO } from "date-fns";
Then, in the description of add, change the help text of --due so
it shows what a date looks like, and add a check at the top of the
action that refuses a date that is not one. The changed and new lines
are marked:
index.tsts
.option("--due <date>", "when the tasks are due, like 2026-10-30") .action(async (titles: string[], options: { due?: string }) => { if (options.due !== undefined && !isValid(parseISO(options.due))) { console.log(chalk.red(`Not a date: ${options.due}. Try 2026-10-30`)); return; } if (titles.length === 0) {
options.due is undefined when you leave --due out, and a line
with no date is fine, so the check runs only when there is one.
return ends the action there: nothing is added and nothing is
saved. The date is kept as you typed it, "2026-10-30": a string, as
due already is in TaskKit's Task, so tasks.json and the tests
stay as they were.
2
Print the date and the days left
In print.ts, under the import of chalk, import what the list
needs:
print.tsts
import { differenceInCalendarDays, format, parseISO } from "date-fns";
At the end of the file, add a function that turns a task's due date
into the text the list prints:
print.tsts
export function dueText(due: string | undefined, today: Date): string { if (due === undefined) { return "not set"; } const date = parseISO(due); const day = format(date, "EEE d MMM"); const days = differenceInCalendarDays(date, today); if (days === 0) { return `${day}, today`; } if (days > 0) { return `${day}, in ${pluralize("day", days, true)}`; } return `${day}, ${pluralize("day", -days, true)} late`;}
A minus in front of a number flips its sign, so when days is -2,
-days is 2, and pluralize gets a count it can read. today is a
parameter rather than a date the function makes itself, and the next
step shows why. In printList, the line that made due now calls
it, with new Date(), which with nothing in the brackets is this
moment:
print.tsts
const due = chalk.dim(task.due ?? "not set"); const due = chalk.dim(dueText(task.due, new Date()));
3
Test it on a Tuesday you choose
A test that used the real today would pass on Tuesday and fail on
Wednesday, when Friday is 2 days away. With today as a parameter,
the test picks its own Tuesday. Make print.test.ts:
print.test.tsts
import { test } from "node:test";import assert from "node:assert/strict";import { parseISO } from "date-fns";import { dueText } from "./print.ts";const tuesday = parseISO("2026-10-27");test("a task due on Friday is due in 3 days", () => { assert.equal(dueText("2026-10-30", tuesday), "Fri 30 Oct, in 3 days");});test("a task due last Sunday is 2 days late", () => { assert.equal(dueText("2026-10-25", tuesday), "Sun 25 Oct, 2 days late");});
dueText gives back plain text and printList colours it, the
rule of lesson 14.3, so the test compares words, not colours.
4
Check, run, and test
Delete tasks.json first: the friday that lesson 14.4 saved in it
is not a date. Then:
bash
npm run checknpm start -- add --due 2026-10-30 "pay rent"npm start -- add --due friday "walk the dog"npm test
The check prints only its two > lines. These outputs are from a run
on Tuesday 27 October 2026. Under the banner, the first add prints
the list with a real date:
Outputtext
Added: Pay rent1 task added, room for 4 moreList 20% fullYour list, A to Z:#1 [todo] Pay rent, due: Fri 30 Oct, in 3 days
The days are counted from your own today, so yours will say
something else. The second add is refused in red, and the list
under it is unchanged:
Outputtext
Not a date: friday. Try 2026-10-301 task added, room for 4 moreList 20% fullYour list, A to Z:#1 [todo] Pay rent, due: Fri 30 Oct, in 3 days
The tests end with ℹ pass 16 and ℹ fail 0: the 14 from before,
and the two new ones.
npm start help add shows the new help text of --due, so whoever
types it learns the form from the help. Its Options part now reads:
Outputtext
Options: --due <date> when the tasks are due, like 2026-10-30 -h, --help display help for command
Predict before you run
The rent was not paid on Friday. On Saturday 31 October, you add one
more task:
Predict
What does the list print after due: for Pay rent?
bash
npm start add "buy milk"
The common mistake: a date nobody checked
parseISO and format are the two functions you see working, so it
is easy to write them and leave isValid out.
Common mistake
Saving a date without checking it
add keeps whatever comes after --due, with no check:
TaskKit adds the task and saves "due": "friday". Then the list asks
format to print a date that holds no moment, format stops with an
error, and TaskKit's catch prints its message in red:
Outputtext
Added: Walk the dog1 task added, room for 4 moreList 20% fullYour list, A to Z:Invalid time value
friday is in tasks.json now, so every later run that prints the
list stops on the same line.
Invalid time value
The fix
Check the date where it comes in, before anything is saved, as the
first step above does:
index.tsts
if (options.due !== undefined && !isValid(parseISO(options.due))) { console.log(chalk.red(`Not a date: ${options.due}. Try 2026-10-30`)); return; }
Invalid time value is date-fns saying "this Date holds no moment".
When you see it, look for a date that was never checked with
isValid, and for the file it may already be saved in: here,
delete the task's "due" line from tasks.json, or the whole file.
Pass all four tasks to complete the lesson with practice. Reach the end without them and it counts as read only.
Exercises
date-fns is not installed on this page, so the exercises read its
patterns, use the built-in Date, write the part of dueText that
turns a number of days into words, and count calendar days by hand.
Exercise · Read the code
Which pattern prints the date with its year
due holds 30 October 2026. Which line prints 30 Oct 2026?
Exercise · Fix the error
The month counts from 0
newYear should be Friday 1 January 2027, and the program should
print Fri Jan 01 2027. It prints a day in February instead. Fix the
numbers on the first line.
Loading the editor…
Exercise · Write the code
Days in words
Write the body of daysText. It gets what differenceInCalendarDays
gives back and returns the words for the list: today for 0,
tomorrow for 1, in 3 days for 3, 1 day late for -1 and
2 days late for -2.
Loading the editor…
Exercise · Predict the result
Count the calendar days
A task is due on Wednesday at 8:00. It is Monday, 22:00. What does
differenceInCalendarDays give for the two, Wednesday first? Only 34
hours lie between them.
Recap
Recap · 3 questions
Check yourself
Why does TaskKit ask for 2026-03-04 rather than 03/04?
Check yourself
What is isValid for?
Check yourself
Why does dueText take today as a parameter?
0 of 0 answered.
A cheat sheet to keep
Cheat sheet
date-fns: working with due dates
What things are
Word
What it means
Date
JavaScript's object for one moment in time
Invalid Date
A Date that holds no moment at all
date-fns
A package of small functions for dates
ISO 8601
Year, month, day: 2026-10-30
Calendar days
How many times the date changes between two days
Code
Write
What you get
npm install date-fns
date-fns in package.json and the lock
new Date(2026, 9, 30)
30 October: months count from 0
new Date()
This moment
parseISO("2026-10-30")
A Date, midnight on your computer
isValid(date)
true if it holds a real moment
due.toDateString()
Fri Oct 30 2026: weekday first
format(due, "EEE d MMM")
Fri 30 Oct
differenceInCalendarDays
How often the date changes; below 0 if late
differenceInDays
Whole 24 hours from the second to the first
Rules
Rule
What it means
Check where it comes in
isValid before you save a date
MM is not mm
Upper case month, lower case minutes
Days left in calendar days
Count dates, not 24 hours
Keep today a parameter
A test picks its own today
Invalid time value
A date nobody checked reached format
Where to go next
You can now turn a typed date into a real one with parseISO, refuse
one that is not a date with isValid, print it with format, and
count the days left with differenceInCalendarDays.
Next is lodash: ready-made helpers, with types from
@types/lodash. date-fns brought its own types; lodash does not,
so that lesson installs a second package for them, as lesson 14.1
showed.
Before you move on, make --due tomorrow work. In index.ts, import
addDays and format beside isValid and parseISO. In add, when
options.due is "tomorrow", replace it with
format(addDays(new Date(), 1), "yyyy-MM-dd") before the check:
addDays is one more date-fns function, and it gives back a new date
that many days later. Then run npm start -- add --due tomorrow "buy milk" and read the list.