date-fns
date-fns copied to clipboard
Typescript typing errors with Deno
Using date-fns with typescript and Deno 1.0 produces the following error:
Uncaught URIError: relative import path "date-fns" not prefixed with / or ./ or ../ Imported from "https://cdn.pika.dev/-/[email protected]/dist=es2019,mode=types/typings.d.ts"
Reproduce using this gist
deno run https://gist.githubusercontent.com/sambs/d3c30151b9ed1cd3d308883148f88aec/raw/acffa68d532bd0df333c466671eb0565e3d85445/date-fns-deno.ts
You should use the 3rd party modules.. It works fine:
import { format, formatDistance, formatRelative, subDays, subHours } from "https://deno.land/x/date_fns/index.js";
import { enUS } from "https://deno.land/x/date_fns/locale/index.js";
const test1 = format(new Date(), "'Today is a' iiii", { locale: enUS });
console.log(test1);
//=> "Today is a Tuesday"
const d = subHours(subDays(new Date(), 1), 8);
console.log(d);
const test2 = formatDistance(d, new Date(), { addSuffix: true, locale: enUS })
console.log(test2);
//=> "3 days ago"
const test3 = formatRelative(subDays(new Date(), 3), new Date(), { locale: enUS })
console.log(test3);
//=> "last Friday at 7:26 p.m."
While the above examples work for me (thanks @sspilleman), the following is still giving me errors:
import { eachDayOfInterval } from "https://deno.land/x/date_fns/index.js";
let a = eachDayOfInterval({
start: new Date(2014, 9, /* Oct */ 6),
end: new Date(2014, 9, /* Oct */ 12),
});
The code works fine, but VSCode (with the deno plugin) marks it with errors.
deno imports appear to be broken entirely now. This snippet:
import * as d from 'https://deno.land/x/[email protected]/index.js'
gives the following error:
error: Import 'https://deno.land/x/[email protected]/getDay.js' failed: 404 Not Found
at https://deno.land/x/[email protected]/nextDay/index.ts:2:0
*edit the index.js import appears to be broken, as does any import that references a index.js file in a subdirectory without being explicit. Importing individual modules still works
I created a PR a few days ago to get this project in better shape for Deno usage, check it out #2545
It seems the custom module https://deno.land/x/date_fns is not being updated. The related repository has been archived.
I tried using the latest verison of date-fns directly and it seemed to work without any problems:
import * as dateFns from "https://cdn.skypack.dev/date-fns@^2.29.2";
const date = new Date();
dateFns.format(date, "yyyy-MM-dd");
dateFns.startOfWeek(date);
So is there anything left to do here?
wanted to throw this out there, deno standard library includes its own datetime library now. Looks like it can handle most things https://deno.land/[email protected]/datetime/mod.ts
import * as datetime from "https://deno.land/[email protected]/datetime/mod.ts";
const date = new Date()
datetime.format(date, 'yyyy-MM-dd')
datetime.difference(new Date("2020/1/1"),new Date("2020/2/2"),{ units : ["days","months"] })
It works now with "npm:date-fns" like this:
import { startOfDay } from "npm:date-fns";
But for some reason if you use esm.sh or Skypack, there are no types, everything is any like this:
import { startOfDay } from "https://esm.sh/date-fns";
I don't know what is wrong with esm.sh or skypack, but if you can use npm imports in Deno, use those. I can't use them because esbuild doesn't support those yet.
unpkg works, with types
import {
addDays,
format,
formatDistance,
formatRelative,
parse,
subDays,
subHours,
} from "https://unpkg.com/[email protected]/esm//index.js";
// import { nl, enUS } from "https://unpkg.com/[email protected]/esm//locale/index.js";
import nl from "https://unpkg.com/[email protected]/esm//locale/nl/index.js";
import enUS from "https://unpkg.com/[email protected]/esm//locale/en-US/index.js";
Not only esm.sh doesn't work, it freezes whole vscode for me.