diff --git a/content/get-started/writing-on-github/working-with-advanced-formatting/displaying-relative-time.md b/content/get-started/writing-on-github/working-with-advanced-formatting/displaying-relative-time.md new file mode 100644 index 000000000000..964614533923 --- /dev/null +++ b/content/get-started/writing-on-github/working-with-advanced-formatting/displaying-relative-time.md @@ -0,0 +1,66 @@ +--- +title: Displaying relative time +intro: You can use the `` element to display timestamps that are localized to the reader and automatically update over time. +product: '{% data reusables.gated-features.markdown-ui %}' +versions: + fpt: '*' + ghes: '*' + ghec: '*' +shortTitle: Relative time +category: + - Write and format content +--- + +## About relative time + +The `` 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. + +## Adding a relative timestamp + +Add a `` 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. + +```html +April 1, 2014 +``` + +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). + +> [!NOTE] +> Always include meaningful fallback text. The fallback is displayed when JavaScript is unavailable, and is used by screen readers and search engines. + +## How the timestamp displays + +Depending on when the timestamp is viewed, the element's text may be replaced with a format such as: + +* next month +* in 2 weeks +* in 5 days +* tomorrow +* in 4 hours +* in 7 minutes +* now +* 1 minute ago +* 30 minutes ago +* 1 hour ago +* 20 hours ago +* yesterday +* 6 days ago +* 2 weeks ago +* on Apr 1, 2014 + +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. + +## Suggesting a language + +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. + +```html +2019年8月12日 +``` + +## Further reading + +* [`relative-time-element` usage documentation](https://github.com/github/relative-time-element#usage) +* [Relative time component in Primer](https://primer.style/product/components/relative-time/) +* [{% data variables.product.prodname_dotcom %} Flavored Markdown Spec](https://github.github.com/gfm/) +* [AUTOTITLE](/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax) diff --git a/content/get-started/writing-on-github/working-with-advanced-formatting/index.md b/content/get-started/writing-on-github/working-with-advanced-formatting/index.md index 1cfd414031ea..1c1ecabeb9c4 100644 --- a/content/get-started/writing-on-github/working-with-advanced-formatting/index.md +++ b/content/get-started/writing-on-github/working-with-advanced-formatting/index.md @@ -14,6 +14,7 @@ children: - /creating-and-highlighting-code-blocks - /creating-diagrams - /writing-mathematical-expressions + - /displaying-relative-time - /autolinked-references-and-urls - /attaching-files - /about-tasklists