Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't get relative time with locale #34

Open
fredt34 opened this issue Nov 7, 2023 · 1 comment
Open

Can't get relative time with locale #34

fredt34 opened this issue Nov 7, 2023 · 1 comment

Comments

@fredt34
Copy link

fredt34 commented Nov 7, 2023

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:

  • Time is Ok
  • relative is Ok with standard language
  • Time displays the date correctly in another locale (case 3 - but only with a dayjs date), not with a regular date (case 4)
  • Last test to display a relative date in another locale triggers an Error 500
<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
Copy link
Owner

metonym commented Mar 31, 2024

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")} />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants