metonym / svelte-time

Svelte component and action to format a timestamp using day.js
https://metonym.github.io/svelte-time
MIT License
137 stars 8 forks source link

Can't get relative time with locale #34

Open fredt34 opened 11 months ago

fredt34 commented 11 months ago

I installed Svelte with bun, it uses recent versions:

    "@sveltejs/adapter-auto": "^2.1.1",
    "@sveltejs/kit": "^1.27.3",
    "flowbite-svelte": "^0.44.19",
    "svelte": "^4.2.2",
    "svelte-check": "^3.5.2",
    "svelte-time": "^0.8.0",
    "vite": "^4.5.0"

It runs in a lxd container.

I created this test pages +page.svelte to test various cases:

<script context="module">
    import "dayjs/esm/locale/uk"
    import dayjs from "dayjs/esm"

    dayjs.locale("uk") // Ukrainian locale to be sure

</script>

<script>js
    import Time from "svelte-time"

    let djsDate = dayjs()
    let djsDateFormatted = dayjs().format("DD MMMM YY")
</script>

ok <Time timestamp="2020-01-01" /><br />
ok <Time relative timestamp="2020-01-01" /><br />
ok <Time timestamp={dayjs("2020-01-01")} format="MMMM" /><br />
oko? <Time timestamp="2020-01-01" format="MMMM" /><br />
ko 
<!-- <Time relative timestamp={dayjs("2020-01-01")} format="MMMM" /><br /> -->
<!-- TypeError: __vite_ssr_import_1__.dayjs(timestamp).from is not a function. (In '__vite_ssr_import_1__.dayjs(timestamp).from()', '__vite_ssr_import_1__.dayjs(timestamp).from' is undefined) -->

Last lines triggers a console error: TypeError: __vite_ssr_import_1__.dayjs(timestamp).from is not a function. (In '__vite_ssr_import_1__.dayjs(timestamp).from()', '__vite_ssr_import_1__.dayjs(timestamp).from' is undefined)

I'm quite starting tinkering with svelte and javascript, so I might have missed something obvious (or my config is too exotic ?) All my other tests about Svelte / Kit are ok.

Plus, I'm a bit surprised that not every date applies the locale defined? Is a format really mandatory?

metonym commented 6 months ago

Apologies for the late reply.

Based on the error dayjs(timetsamp).from is not a function, I suspect the issue may be related to dayjs not having yet used the relativeTime plugin (which is needed for the relative time aspect).

I've also since updated the README to fix the custom locale instructions.

For me, the following prints out 4 роки тому.

<script>
  import "dayjs/locale/uk";

  // Apply the locale globally
  dayjs.locale("uk");

  import Time, { dayjs } from "svelte-time";
</script>

<Time relative timestamp={dayjs("2020-01-01")} />