Back to packages list

Vals using date-fns

Description from the NPM package:
Modern JavaScript date utility library
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import { fetchPelotonWorkouts } from "https://esm.town/v/andreterron/fetchPelotonWorkouts";
import { email } from "https://esm.town/v/std/email?v=9";
import process from "node:process";
import { isSameDay } from "npm:date-fns";
export default async function checkTodayWorkout() {
const data = await fetchPelotonWorkouts(
"3f5bf759bb65494d8b35d26ec72b7340",
process.env.pelotonSessionId,
);
const latestWorkoutDate = new Date(data.data[0].created_at * 1000);
const didWorkOutToday = isSameDay(latestWorkoutDate, new Date());
if (didWorkOutToday) {
return;
}
email({
text: "You did not work out today",
subject: "It's time to sweat",
});
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env -S deno run -A
import type * as sunbeam from "https://deno.land/x/sunbeam/mod.ts";
import * as dates from "npm:date-fns";
const manifest = {
title: "Deno Deploy",
description: "Manage your Deno Deploy projects",
preferences: [
{
name: "token",
label: "Access Token",
type: "text",
},
],
commands: [
{
name: "projects",
title: "List Projects",
mode: "filter",
},
{
name: "dashboard",
title: "Open Dashboard",
mode: "silent",
},
{
name: "deployments",
title: "List Deployments",
hidden: true,
mode: "filter",
params: [{ name: "project", label: "Project", type: "text" }],
},
{
name: "playground",
title: "View Playground",
hidden: true,
mode: "detail",
params: [{ name: "project", label: "Project", type: "text" }],
},
],
} as const satisfies sunbeam.Manifest;
if (Deno.args.length == 0) {
console.log(JSON.stringify(manifest));
Deno.exit(0);
}
const payload: sunbeam.Payload<typeof manifest> = JSON.parse(Deno.args[0]);
const deployToken = payload.preferences.token;
if (!deployToken) {
console.error("Missing deploy token");
Deno.exit(1);
}
// #sunbeam
try {
const res = await run(payload);
if (res) {
console.log(JSON.stringify(res));
}
} catch (e) {
console.error(e);
Deno.exit(1);
}
async function run(payload: sunbeam.Payload<typeof manifest>) {
switch (payload.command) {
case "dashboard": {
await new Deno.Command("sunbeam", {
args: ["open", "https://dash.deno.com"],
}).output();
return;
}
case "projects": {
const resp = await fetchDeployAPI("/projects");
if (resp.status != 200) {
throw new Error("Failed to fetch projects");
}
const projects = await resp.json();
return {
items: projects.map((project: any) => {
const item: sunbeam.ListItem = {
title: project.name,
accessories: [project.type],
actions: [
{
title: "Open Dashboard",
type: "open",
open: {
url: `https://dash.deno.com/projects/${project.id}`,
},
},
],
};
if (project.hasProductionDeployment) {
const domains = project.productionDeployment.deployment.domainMappings;
const domain = domains.length
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { weekRange as weekRange2 } from "https://esm.town/v/andreterron/weekRange";
import { WorkoutIcon } from "https://esm.town/v/andreterron/workoutIcons";
import { addDays, format } from "npm:date-fns@2.30.0";
import { formatInTimeZone, utcToZonedTime } from "npm:date-fns-tz";
export function weekWorkoutIcons(
byDay: Record<string, any>,
timezone: string = "America/Los_Angeles",
): WorkoutIcon[] {
const todayKey = formatInTimeZone(
new Date(),
timezone,
"yyyy-MM-dd",
);
const weekRange = weekRange2(timezone);
return weekRange.map((day): WorkoutIcon => {
const key = format(day, "yyyy-MM-dd");
const previousKey = format(addDays(day, -1), "yyyy-MM-dd");
const workedOut = !!byDay[key];
const past = key < todayKey;
if (workedOut) return "done";
// Never skip twice
if (past && !workedOut && !byDay[previousKey]) return "failed";
if (past) return "skipped";
if (key === todayKey) return "today";
return "future";
});
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import { eachDayOfInterval, endOfWeek, startOfDay, startOfWeek } from "npm:date-fns@2.30.0";
import { formatInTimeZone, utcToZonedTime } from "npm:date-fns-tz@2.0.0";
export const weekRange = (timezone: string = "America/Los_Angeles") => {
const now = new Date();
const nowTZ = new Date(
formatInTimeZone(now, timezone, "yyyy-MM-dd"),
);
// return now;
const result = eachDayOfInterval({
start: startOfWeek(nowTZ, { weekStartsOn: 1 }),
end: endOfWeek(nowTZ, { weekStartsOn: 1 }),
});
return result;
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { weekRange as weekRange2 } from "https://esm.town/v/andreterron/weekRange";
import { workedOutByDay } from "https://esm.town/v/andreterron/workedOutByDay";
import { addDays, format } from "npm:date-fns@2.30.0";
import { formatInTimeZone, utcToZonedTime } from "npm:date-fns-tz@2.0.0";
export const workoutDays = async (
byDay: Record<string, any>,
timezone: string = "America/Los_Angeles",
) => {
const todayKey = formatInTimeZone(
new Date(),
timezone,
"yyyy-MM-dd",
);
const weekRange = await weekRange2();
return weekRange.map((day) => {
const key = format(day, "yyyy-MM-dd");
const previousKey = format(addDays(day, -1), "yyyy-MM-dd");
const workedOut = !!byDay[key];
const past = key < todayKey;
return {
day: key,
workedOut,
// Timezones are hard, so we compare strings
past,
today: key === todayKey,
failed: past && !workedOut && !byDay[previousKey],
};
});
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import { myPelotonWorkouts } from "https://esm.town/v/andreterron/myPelotonWorkouts";
import { format } from "npm:date-fns@2.30.0";
import { utcToZonedTime } from "npm:date-fns-tz@2.0.0";
export const workedOutByDay = async (timezone: string = "America/Los_Angeles") => {
const workouts = await myPelotonWorkouts();
const byDay: {
[k: string]: any;
} = {};
for (let w of workouts) {
byDay[
format(
utcToZonedTime(new Date(w.start_time * 1000), timezone),
"yyyy-MM-dd",
)
] = w;
}
return byDay;
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
type LineType = "header" | "Daily Offerings" | "date";
export async function parseMenuDate(text: string) {
const dateFns = await import("npm:date-fns");
let type: LineType;
let firstBreakAt = text.indexOf("?");
if (firstBreakAt == -1) {
if (text === "Daily Offerings") {
type = "Daily Offerings";
}
else {
type = "header";
}
return { type };
}
let prefix = text.substring(0, firstBreakAt);
let suffix = text.substring(firstBreakAt + 1);
const date = dateFns.parse(prefix, "MMMM d; yyyy", new Date());
type = "date";
return {
type,
date,
prefix,
suffix,
};
}
1
2
3
4
5
export const timestampInLastDay = async (timestamp) => {
const { isAfter, subDays } = await import("npm:date-fns");
console.log("timestampInLastDay timestamp", timestamp);
return isAfter(new Date(timestamp), subDays(Date.now(), 1));
};

date-fns

date-fns is a much better alternative to moment. It's much smaller, simpler, and easier to use. Plus, it supports ESM - notice how I can just import add and formatRelative here! Use it for all your date needs.

1
2
3
4
export let datefnsExample = (async () => {
const { add, formatRelative } = await import("npm:date-fns");
return formatRelative(add(new Date(), { minutes: 10 }), new Date());
})();
1
2
3
4
5
6
7
8
9
10
11
12
import { fetch } from "https://esm.town/v/std/fetch";
export let chessTime = (async () => {
const { secondsToHours } = await import("npm:date-fns");
const stats = await fetch("https://lichess.org/api/user/tmcw").then((r) =>
r.json()
);
return `I've spent ${
(secondsToHours(stats.playTime.total) / 24).toFixed(2)
} days playing chess.
In that time, I could have written a novel that defined my generation.`;
})();
1
2
3
4
5
6
export let getWeekNumber = (async () => {
const _ = await import("npm:date-fns");
const date = new Date();
const weekNumber = _.getISOWeek(date);
return "Semaine " + weekNumber;
})();
1
2
3
4
export const formatDateToRoam = async (date: Date) => {
const { format } = await import("npm:date-fns");
return format(date, "MMMM do, yyyy");
};

Every Monday

Prints every monday between 2 dates. I use it for quarterly planning.

The output can be pasted in Notion as a bullet list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
const everyMonday = (async () => {
// --- Imports ---
const {
addDays,
startOfWeek,
startOfDay,
isSameDay,
isAfter,
isBefore,
format,
} = await import("npm:date-fns");
// --- Constants ---
// End of June
const from = new Date(2024, 0, 1);
// Beginning of October
const until = startOfDay(new Date(2024, 3, 7));
// --- Loop ---
let d = startOfWeek(from, { weekStartsOn: 1 });
const mondays: Date[] = [];
while (!isAfter(d, until)) {
if (!isBefore(d, from)) {
mondays.push(d);
}
d = startOfDay(addDays(d, 7));
}
// --- Format ---
return mondays.map((d) => format(d, "- MMM d")).join("\n\n");
})();
console.log(await everyMonday);
1
Next