All Projects → metonym → svelte-time

metonym / svelte-time

Licence: MIT license
Format a timestamp using day.js

Programming Languages

typescript
32286 projects
Svelte
593 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to svelte-time

Hls Trimming Frame Accuracy
JS Code that given a group of HLS chunks, a start timestamp, and end timestamp it creates one MP4 that contains the original V/A frame accuracy trimmed and perfectly aligned
Stars: ✭ 46 (-34.29%)
Mutual labels:  timestamp
Tj
stdin line timestamps. single binary, no dependencies. osx & linux & windows. plays well with jq.
Stars: ✭ 218 (+211.43%)
Mutual labels:  timestamp
gatsby-remark-relative-images
Convert markdown image src(s) to be relative for gatsby-remark-images.
Stars: ✭ 79 (+12.86%)
Mutual labels:  relative
Quanta
high-speed timing library in Rust
Stars: ✭ 104 (+48.57%)
Mutual labels:  timestamp
Eztime
ezTime — pronounced "Easy Time" — is a very easy to use Arduino time and date library that provides NTP network time lookups, extensive timezone support, formatted time and date strings, user events, millisecond precision and more.
Stars: ✭ 173 (+147.14%)
Mutual labels:  timestamp
Jsrsasign
The 'jsrsasign' (RSA-Sign JavaScript Library) is an opensource free cryptography library supporting RSA/RSAPSS/ECDSA/DSA signing/validation, ASN.1, PKCS#1/5/8 private/public key, X.509 certificate, CRL, OCSP, CMS SignedData, TimeStamp, CAdES JSON Web Signature/Token in pure JavaScript.
Stars: ✭ 2,760 (+3842.86%)
Mutual labels:  timestamp
Fliplog
fluent logging with verbose insight, colors, tables, emoji, filtering, spinners, progress bars, timestamps, capturing, stack traces, tracking, presets, & more...
Stars: ✭ 41 (-41.43%)
Mutual labels:  timestamp
ngx-timeago
⏰ Live updating timestamps in Angular 6+
Stars: ✭ 70 (+0%)
Mutual labels:  timestamp
Opentimestamps Client
OpenTimestamps client
Stars: ✭ 197 (+181.43%)
Mutual labels:  timestamp
svelte-popper
🍿🔗Official Svelte wrapper for Popper - the positioning library
Stars: ✭ 27 (-61.43%)
Mutual labels:  svelte-component
Time Stamp
Get a formatted timestamp. Used in gulp, assemble, generate, and many others.
Stars: ✭ 104 (+48.57%)
Mutual labels:  timestamp
Dategrep
print lines matching a time range
Stars: ✭ 159 (+127.14%)
Mutual labels:  timestamp
rtss
Relative TimeStamps for Stuff
Stars: ✭ 42 (-40%)
Mutual labels:  timestamp
Snow Stamp
Get the timestamp from a Discord snowflake ❄
Stars: ✭ 95 (+35.71%)
Mutual labels:  timestamp
time decode
A timestamp and date decoder written for python 3
Stars: ✭ 24 (-65.71%)
Mutual labels:  timestamp
Pg variables
Session wide variables for PostgreSQL
Stars: ✭ 44 (-37.14%)
Mutual labels:  timestamp
Jest Date Mock
🌗 Mock `Date` when run unit test cases with jest. Make tests of Date easier.
Stars: ✭ 224 (+220%)
Mutual labels:  timestamp
svelte-readotron
Svelte component to display an estimated reading time
Stars: ✭ 36 (-48.57%)
Mutual labels:  svelte-component
praecox-datepicker
A date picker built with Svelte.Simple and flexible, supporting functions such as single selection, multiple selection, disabling, and marking.
Stars: ✭ 66 (-5.71%)
Mutual labels:  svelte-component
react-modern-datepicker
A modern date picker for react library
Stars: ✭ 19 (-72.86%)
Mutual labels:  dayjs

svelte-time

NPM

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

This utility wraps the date-time library day.js as a declarative Svelte component and action.

Use cases

  • format a timestamp using the semantic time element
  • display a human-readable, relative time (e.g., "4 days ago") while preserving the original timestamp

Sample output:

<time title="May 15, 2022" datetime="2022-05-15T18:03:57.430Z">
  a few seconds ago
</time>

Try it in the Svelte REPL.


Installation

# Yarn
yarn add -D svelte-time

# npm
npm i -D svelte-time

# pnpm
pnpm i -D svelte-time

Usage

Usage with vite

If using [email protected] with a vite-only set-up, include "dayjs/plugin/relativeTime.js" in optimizeDeps.include.

See examples/vite.

// vite.config.js
import { svelte } from "@sveltejs/vite-plugin-svelte";
import { defineConfig } from "vite";

export default defineConfig(() => {
  return {
    plugins: [svelte()],
    optimizeDeps: {
      include: ["dayjs/plugin/relativeTime.js"],
    },
  };
});

Time component

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

<script>
  import Time from "svelte-time";
</script>

<Time />

The timestamp prop can be any of the following dayjs values: string | number | Date | Dayjs.

<Time timestamp="2020-02-01" />

<Time timestamp={new Date()} />

<Time timestamp={1e10} />

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

<Time timestamp="2020-02-01" format="dddd @ h:mm A · MMMM D, YYYY" />

<Time timestamp={new Date()} format="YYYY/MM/DD" />

<Time timestamp={1e10} format="ddd" />

Relative time

Set the relative prop value to true for the relative time displayed in a human-readable format.

<Time relative />

<Time relative timestamp="2021-02-02" />

<Time relative timestamp={1e10} />

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

Use the format prop to customize the format.

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

Live updates

Set live to true for a live updating relative timestamp. The default refresh interval is 60 seconds.

<Time live relative />

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

<!-- Update every 30 seconds -->
<Time live={30 * 1_000} relative />

<!-- Update every 10 minutes -->
<Time live={10 * 60 * 1_000} relative />

svelteTime action

Use the svelteTime action to format a timestamp in a raw HTML element.

<script>
  import { svelteTime } from "svelte-time";
</script>

<time use:svelteTime />

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

<time
  use:svelteTime={{
    relative: true,
    timestamp: "2021-02-02",
  }}
/>

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

Similar to the Time component, the live prop only works with relative time.

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

Specify a custom update interval using the live prop.

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

Custom locale

Load a custom locale and set it as the default locale using the dayjs.locale API.

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

  dayjs.locale("de"); // German locale
</script>

<script>
  import Time from "svelte-time";
</script>

<Time />

dayjs export

dayjs is re-exported for your convenience. This is useful when the component and action would not work for programmatic usage, like setting the document title.

Note: the exported dayjs function already extends the relativeTime plugin.

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

<button on:click={() => (document.title = dayjs().format("MMM DD, YYYY"))}> Set title </button>

API

Props

Name Type Default value
timestamp string | number | Date | Dayjs new Date().toISOString()
format string "MMM DD, YYYY" (See dayjs display format)
relative boolean false
live boolean | number false
formatted string ""

Examples

The examples folder contains sample set-ups.

TypeScript

Svelte version 3.31 or greater is required to use this component with TypeScript.

TypeScript definitions are located in the types folder.

Changelog

CHANGELOG.md

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].