Skip to content

Commit

Permalink
feat: add formatDate function
Browse files Browse the repository at this point in the history
  • Loading branch information
iyxan23 committed Sep 21, 2024
1 parent b650717 commit dcefb1d
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/sheet/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { success } from "./expression/result";
import { startOfDay } from "date-fns/fp/startOfDay";
import { differenceInDays } from "date-fns/fp/differenceInDays";
import { differenceInSeconds } from "date-fns/fp/differenceInSeconds";
import { formatDate } from "date-fns/format";

// == array

Expand Down Expand Up @@ -223,6 +224,10 @@ const writeDate = createTemplaterFunction(z.tuple([z.coerce.date()]), (date) =>
const writeTime = createTemplaterFunction(z.tuple([z.coerce.date()]), (date) =>
success(differenceInSeconds(startOfDay(date), date) / (60 * 60 * 24)),
);
const formatDate_ = createTemplaterFunction(
z.tuple([z.coerce.date(), z.string().optional()]),
(date, format = "yyyy-MM-dd") => success(formatDate(date, format)),
);

export const builtinFunctions = {
array,
Expand Down Expand Up @@ -257,4 +262,5 @@ export const builtinFunctions = {
now,
writeDate,
writeTime,
formatDate: formatDate_,
};

0 comments on commit dcefb1d

Please sign in to comment.