Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Svelte component and action to format a timestamp using day.js

License

NotificationsYou must be signed in to change notification settings

metonym/svelte-time

Repository files navigation

NPM

Note:svelte-time@2.0.0 only supports Svelte 5 in Runes mode.

Usesvelte-time@1.0.0 for Svelte 3, 4, and 5 (non-Runes mode).


svelte-time is a Svelte component and action to make a timestamp human-readable while encoding the machine-parseable value in the semantictime element.

Under the hood, it usesday.js, a lightweight date-time library.

<!-- Input --><Timerelative /><!-- Output rendered in the DOM --><timetitle="May 15, 2022"datetime="2022-05-15T18:03:57.430Z">  a few seconds ago</time>

Try it in theSvelte REPL.


Installation

# npmnpm i svelte-time# pnpmpnpm i svelte-time# Bunbun i svelte-time# Yarnyarn add svelte-time

Usage

Time component

The displayed time defaults tonew Date().toISOString() and is formatted as"MMM DD, YYYY".

<script>importTimefrom"svelte-time";</script><Time />

Thetimestamp prop can be any of the followingdayjs values:string | number | Date | Dayjs.

<Timetimestamp="2020-02-01" /><Timetimestamp={newDate()} /><Timetimestamp={1e10} />

Use theformat prop to format the timestamp. Refer to thedayjs format documentation for acceptable formats.

<Timetimestamp="2020-02-01"format="dddd @ h:mm A · MMMM D, YYYY" /><Timetimestamp={newDate()}format="YYYY/MM/DD" /><Timetimestamp={1e10}format="ddd" />

Relative time

Set therelative prop value totrue for the relative time displayed in a human-readable format.

<Timerelative /><Timerelativetimestamp="2021-02-02" /><Timerelativetimestamp={1e10} />

When using relative time, thetitle attribute will display a formatted timestamp.

Use theformat prop to customize theformat.

<Timerelativeformat="dddd @ h:mm A · MMMM D, YYYY" />

When usingrelative, thetime element will set the formatted timestamp as thetitle attribute. Specify a customtitle to override this.

<Timerelativetitle="Custom title" />

Set the value toundefined to omit thetitle altogether.

<Timerelativetitle={undefined} />

Live updates

Setlive totrue for a live updating relative timestamp. The default refresh interval is 60 seconds.

<Timeliverelative />

To customize the interval, pass a value tolive in milliseconds (ms).

<!-- Update every 30 seconds --><Timelive={30*1_000}relative /><!-- Update every 10 minutes --><Timelive={10*60*1_000}relative />

svelteTime action

An alternative to theTime component is to use thesvelteTime action to format a timestamp in a raw HTML element.

The API is the same as theTime component.

<script>import {svelteTime }from"svelte-time";</script><timeuse:svelteTime></time><timeuse:svelteTime={{timestamp:"2021-02-02",format:"dddd @ h:mm A · MMMM D, YYYY",  }}></time>

Relative time

Setrelative totrue to use relative time.

<timeuse:svelteTime={{relative:true,timestamp:"2021-02-02",  }}></time><timeuse:svelteTime={{relative:true,timestamp:"2021-02-02",format:"dddd @ h:mm A · MMMM D, YYYY",  }}></time>

To customize or omit thetitle attribute, use thetitle prop.

<timeuse:svelteTime={{relative:true,title:"Custom title",timestamp:"2021-02-02",  }}></time><timeuse:svelteTime={{relative:true,title:undefined,timestamp:"2021-02-02",  }}></time>

Similar to theTime component, thelive prop only works with relative time.

<timeuse:svelteTime={{relative:true,live:true,  }}></time>

Specify a custom update interval using thelive prop.

<timeuse:svelteTime={{relative:true,live:30*1_000,// Update every 30 seconds  }}></time>

dayjs export

Thedayjs library is exported from this package for your convenience.

Note: the exporteddayjs function already extends therelativeTime plugin.

<script>import {dayjs }from"svelte-time";let timestamp=$state("");</script><buttononclick={()=> (timestamp=dayjs().format("HH:mm:ss.SSSSSS"))}>  Update {timestamp}</button>

Custom locale

The defaultdayjs locale is English. No other locale is loaded by default for performance reasons.

To use acustome locale, import the relevant language fromdayjs. See a list ofsupported locales.

<script>import"dayjs/locale/de";// German localeimportTime, {dayjs }from"svelte-time";</script><Timetimestamp={dayjs().locale("de")}format="dddd, MMMM D, YYYY" />

Custom locale (global)

Use thedayjs.locale method to set a custom locale as the default.

<script>import"dayjs/locale/de";// German localeimport {dayjs }from"svelte-time";// Set the default locale to German.dayjs.locale("de");</script>

Custom timezone

To use acustom timezone, import theutc andtimezone plugins fromdayjs.

<script>importutcfrom"dayjs/plugin/utc";importtimezonefrom"dayjs/plugin/timezone";importTime, {dayjs }from"svelte-time";dayjs.extend(utc);dayjs.extend(timezone);</script><Timetimestamp={dayjs("2013-11-18 11:55:20").tz("America/Toronto")}format="YYYY-MM-DDTHH:mm:ss"/>

Custom timezone (global)

Use thedayjs.tz.setDefault method to set a custom timezone as the default.

<script>importutcfrom"dayjs/plugin/utc";importtimezonefrom"dayjs/plugin/timezone";importTime, {dayjs }from"svelte-time";dayjs.extend(utc);dayjs.extend(timezone);dayjs.tz.setDefault("America/New_York");</script>

User timezone

Use thedayjs.ts.guess method to guess the user's timezone.

importutcfrom"dayjs/plugin/utc";importtimezonefrom"dayjs/plugin/timezone";dayjs.extend(utc);dayjs.extend(timezone);dayjs.tz.guess();// America/New_York

To retrieve the abbreviated time zone, extend theadvancedFormat plugin.

  import utc from "dayjs/plugin/utc";  import timezone from "dayjs/plugin/timezone";+ import advancedFormat from "dayjs/plugin/advancedFormat";  import { dayjs } from "svelte-time";  dayjs.extend(utc);  dayjs.extend(timezone);+ dayjs.extend(advancedFormat);

Then, use thedayjs().local method to get the user's local time zone and format it using the"z" advanced option.

dayjs().local().format("z");// ESTdayjs().local().format("zzz");// Eastern Standard Time

API

Props

NameTypeDefault value
timestampstring |number |Date |Dayjsnew Date().toISOString()
formatstring"MMM DD, YYYY" (Seedayjs display format)
relativebooleanfalse
liveboolean |numberfalse
formattedstring""

Examples

Changelog

CHANGELOG.md

License

MIT

About

Svelte component and action to format a timestamp using day.js

Topics

Resources

License

Stars

Watchers

Forks

Contributors4

  •  
  •  
  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp