Skip to content

Commit 2880e29

Browse files
committed
Create docs for the <relative-time> element in advanced markdown formatting
1 parent 8f330f1 commit 2880e29

2 files changed

Lines changed: 67 additions & 0 deletions

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
---
2+
title: Displaying relative time
3+
intro: You can use the `<relative-time>` element to display timestamps that are localized to the reader and automatically update over time.
4+
product: '{% data reusables.gated-features.markdown-ui %}'
5+
versions:
6+
fpt: '*'
7+
ghes: '*'
8+
ghec: '*'
9+
shortTitle: Relative time
10+
category:
11+
- Write and format content
12+
---
13+
14+
## About relative time
15+
16+
The `<relative-time>` element formats an ISO 8601 timestamp as a localized string or as relative text that auto-updates in the reader's browser. The displayed time is adjusted to each reader's timezone, locale, and preferences.
17+
18+
## Adding a relative timestamp
19+
20+
Add a `<relative-time>` element to your Markdown. The element must have a `datetime` attribute set to a valid [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) timestamp, and should include human-readable fallback text as its content.
21+
22+
```html
23+
<relative-time datetime="2014-04-01T16:30:00Z">April 1, 2014</relative-time>
24+
```
25+
26+
A reader may see "3 days ago," "on Apr 1, 2014," or "1 Apr 2014," depending on when they view it and their browser settings. If JavaScript is disabled, the reader sees the fallback text between the tags (`April 1, 2014` in this example).
27+
28+
> [!NOTE]
29+
> Always include meaningful fallback text. The fallback is displayed when JavaScript is unavailable, and is used by screen readers and search engines.
30+
31+
## How the timestamp displays
32+
33+
Depending on when the timestamp is viewed, the element's text may be replaced with a format such as:
34+
35+
* next month
36+
* in 2 weeks
37+
* in 5 days
38+
* tomorrow
39+
* in 4 hours
40+
* in 7 minutes
41+
* now
42+
* 1 minute ago
43+
* 30 minutes ago
44+
* 1 hour ago
45+
* 20 hours ago
46+
* yesterday
47+
* 6 days ago
48+
* 2 weeks ago
49+
* on Apr 1, 2014
50+
51+
A relative date phrase is used for up to a month and then the actual date is shown. Formatting follows the browser's [`Intl.RelativeTimeFormat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat) implementation, so exact phrasing follows the reader's locale.
52+
53+
## Suggesting a language
54+
55+
Add a [`lang`](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/lang) attribute with a language tag to suggest formatting in that language.
56+
57+
```html
58+
<relative-time datetime="2019-08-12T20:50:00Z" lang="zh">2019年8月12日</relative-time>
59+
```
60+
61+
## Further reading
62+
63+
* [`relative-time-element` usage documentation](https://github.com/github/relative-time-element#usage)
64+
* [Relative time component in Primer](https://primer.style/product/components/relative-time/)
65+
* [{% data variables.product.prodname_dotcom %} Flavored Markdown Spec](https://github.github.com/gfm/)
66+
* [AUTOTITLE](/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax)

‎content/get-started/writing-on-github/working-with-advanced-formatting/index.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ children:
1414
- /creating-and-highlighting-code-blocks
1515
- /creating-diagrams
1616
- /writing-mathematical-expressions
17+
- /displaying-relative-time
1718
- /autolinked-references-and-urls
1819
- /attaching-files
1920
- /about-tasklists

0 commit comments

Comments
 (0)