Skip to content
IzzyBlazor

Enter all results ↑ ↓ move Esc close

RelativeTime

Since 0.1.0-alpha.24
Shows a time as how long ago it was, kept current.

A time as how long ago it was, or how soon it is: "2 hours ago", "Yesterday", "In 3 days". Past Threshold it shows the date instead. The server writes the text, so it's right as the page loads, with JavaScript off too; the library's script keeps it current as the page stays open and writes it in the reader's language (the page's lang) and time zone.

How long ago

Set Value to a time, in UTC or with its offset. It reads "2 hours ago" or "Yesterday", with the full date on hover, and stays current while the page is open. It starts with a capital, as it stands alone; within a sentence, set Capitalize to false.

Static

Preview

  • Production

    a41d6e9 Fix the invoice total's rounding

  • Staging

    8c03fc0 Add the cookie settings page

  • Production

    5914cbf Maintenance page

  • Production

    e180c60 Footer links

  • Staging

    7b3137d Pricing tiers

Code

Usage.razor
@* A deploy log: each deploy's time as how long ago, with the full date on hover. The times are
   set from now, so the example always reads as recent. *@
<List Dividers MaxWidth="32.Rem">
    @foreach (var deploy in _deploys)
    {
        <ListItem Icon="Lucide.Rocket" Description="@deploy.Commit">
            <Row Gap="2" Justify="Justify.Between" Wrap>
                <TextBlock As="span" Weight="FontWeight.Medium">@deploy.Target</TextBlock>
                <TextBlock As="span" Size="TextSize.Sm" Color="Color.Muted"><RelativeTime Value="deploy.At" /></TextBlock>
            </Row>
        </ListItem>
    }
</List>

@code {
    private readonly (string Target, string Commit, DateTimeOffset At)[] _deploys =
    [
        ("Production", "a41d6e9 Fix the invoice total's rounding", DateTimeOffset.UtcNow.AddSeconds(-20)),
        ("Staging", "8c03fc0 Add the cookie settings page", DateTimeOffset.UtcNow.AddMinutes(-14)),
        ("Production", "5914cbf Maintenance page", DateTimeOffset.UtcNow.AddHours(-5)),
        ("Production", "e180c60 Footer links", DateTimeOffset.UtcNow.AddDays(-1).AddHours(-2)),
        ("Staging", "7b3137d Pricing tiers", DateTimeOffset.UtcNow.AddDays(-6)),
    ];
}

Static: The times stay current, in the reader's language and time zone.

Add this example to a page with the izzy CLI:

bash
izzy add component RelativeTime --at <file>:<line>

Times far off

Past Threshold, 30 days by default, the date shows instead. Times ahead read "in 3 days". Set Threshold to null to stay relative however far.

Static

Preview

INV-0118
Due
INV-0119
Due
INV-0120
Due
INV-0107
Due

Code

Threshold.razor
@* Invoices due: soon ones read "in 3 days", and past a week the due date shows instead. *@
<DescriptionList MaxWidth="28.Rem">
    @foreach (var invoice in _invoices)
    {
        <DescriptionItem Term="@invoice.Number">
            Due <RelativeTime Value="invoice.Due" Threshold="TimeSpan.FromDays(7)" Capitalize="false" />
        </DescriptionItem>
    }
</DescriptionList>

@code {
    private readonly (string Number, DateTimeOffset Due)[] _invoices =
    [
        ("INV-0118", DateTimeOffset.UtcNow.AddHours(5)),
        ("INV-0119", DateTimeOffset.UtcNow.AddDays(3)),
        ("INV-0120", DateTimeOffset.UtcNow.AddDays(24)),
        ("INV-0107", DateTimeOffset.UtcNow.AddDays(-12)),
    ];
}

Details

Give Value in UTC or with its offset. A DateTime read back from a database often has no kind (EF Core's SQLite provider), and converts to the server's zone: mark it as UTC.

The full date and time is the element's title, shown on hover. Where the exact time matters, such as an audit record, show it on the page as well.

The server's words are IzzyText's Relative… strings; the script's are the browser's, in the page's lang. An app that translates those strings and sets lang sees the same words before and after the script runs. Changing the English strings ("just now") lasts only until the first update.

Mode support

What works in each mode, RelativeTime
FeatureStaticInteractive
EverythingYesYes

Static: static server-side rendering, with no circuit and no WebAssembly. Interactive: Server, WebAssembly or Auto.

Accessibility

  • It renders a time element whose datetime is the exact time, for assistive technology.
  • Updates as time passes are silent: no live region announces them.
  • The title isn't reachable by keyboard or touch, so it repeats the time, never adds to it.

Parameters

RelativeTime parameters
PropTypeDefault
ValueThe time.DateTimeOffset?
ThresholdHow far from now it stays relative; past it, the date shows ("November 3", with the year when it isn't this one).TimeSpan?TimeSpan.FromMilliseconds(2592000000)
CapitalizeStarts it with a capital ("Yesterday", "Just now"), as where it stands alone: a table cell, under a title.booltrue

See also

  • Timeline: shows events on a rail, such as an order's history.
  • Comment: shows a comment in a discussion, with who wrote it, when, and replies nested under it.

Connection lost

Rejoining the server…