GithubHelp home page GithubHelp logo

alexwilson / o-date Goto Github PK

View Code? Open in Web Editor NEW

This project forked from financial-times/o-date

0.0 3.0 0.0 158 KB

Javascript utilities for formatting and updating dates in FT style

Home Page: registry.origami.ft.com/components/o-date

JavaScript 100.00%

o-date's Introduction

o-date Build Status

o-date provides javascript utilities for formatting and updating dates in FT style. This component is mainly useful when you want your dates formatted to express relative time.

Usage

Browser support

This module has been verified in Internet Explorer 8+, modern desktop browsers (Chrome, Safari, Firefox, ...) and mobile browsers (Android browser, iOS safari, Chrome mobile).

Markup

To provide the best non-JS fallback you should markup your dates as follows:

<time data-o-component="o-date" class="o-date" datetime="{{iso8601String}}">{FT formatted date (including time if appropriate)}</time>

This module's format method will also run in Node.js and can be used to populate your model with the appropriate formatted date string. It's not recommended to output the 'time ago' server side as it will not be cacheable and will not update in the browser if the user leaves the page open for a prolonged period of time.

JavaScript

To display dates in the standard relative time format, a o.DOMContentLoaded event can be dispatched on the document to auto-construct each element with a data-o-component="o-date" attribute:

document.addEventListener('DOMContentLoaded', function() {
    document.dispatchEvent(new CustomEvent('o.DOMContentLoaded'));
});

You can also run require('o-date').init() once the DOM has loaded if you don't want to initialise other modules at the same time.

Run require('o-date').init(el) on any elements containing dates that are added to the page after DOM load.

o-date#format(date, tpl)

Returns a date formatted as a string

  • date: A javascript Date object or a valid string to pass to the Date constructor
  • tpl: A string specifying what format to output the date in:
    • 'datetime': formats the date in the standard FT long format, including the time. E.g. May 15, 2014 8:10 am
    • 'date': formats the date in the standard FT long format. E.g. May 15, 2014
    • Any other string using widespread conventions for time/date placeholders, which will be replaced with values extracted from the date provided. See ./main.js for an up to date list of supported formats. To avoid e.g. the mm in common being replaced with the month prefix with a double backslash co\\mmon i.e. In most cases custom date formats should not be used, in favour of the standard FT date and datetime formats

o-date#timeAgo(date)

Returns the relative time since the given date, formatted as a human readable string e.g. 13 minutes ago.

o-date#ftTime(date)

Returns relative time or timestamp for a given date, in accordance with FT date formatting conventions.

  • date: A javascript Date object or a valid string to pass to the Date constructor

o-date#asTodayOrYesterdayOrNothing(date)

Returns 'yesterday', 'today' or '' for a given date. You can request this formatting for o-date components by adding data-o-date-format="today-or-yesterday-or-nothing".

  • date: A javascript Date object or a valid string to pass to the Date constructor

o-date#init(el)

Within a given container element, converts dates to ftTime (see above) and periodically updates their values. Within the container all <time> elements with o-date in data-o-component will be updated. If a given <time> element contains an element with the class o-date__printer the relative time will be output here, otherwise it will replace the contents of the entire <time> element. Once the <time> element has been formatted by o-date, the attribute data-o-date-js is added, enabling conditional styling and/or hiding the date until it is correctly formatted.

  • el: An HTMLElement within which to scan for o-date elements. If the element itself is a <time> element with o-date in data-o-component, then o-date will run directly on this element rather than querying for suitable elements within it.

Contact

If you have any questions or comments about this component, or need help using it, please either raise an issue, visit #ft-origami or email Origami Support.


Licence

This software is published by the Financial Times under the MIT licence.

o-date's People

Contributors

alicebartlett avatar andygout avatar bjfletcher avatar expe avatar glynnphillips avatar i-like-robots avatar j-mes avatar jakechampion avatar kaelig avatar leggsimon avatar mariabg avatar matthew-andrews avatar onishiweb avatar rowanmanning avatar triblondon avatar wheresrhys avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.