GithubHelp home page GithubHelp logo

whatasoda / moment-shortformat Goto Github PK

View Code? Open in Web Editor NEW

This project forked from researchgate/moment-shortformat

0.0 1.0 0.0 83 KB

moment-shortformat.js is an extension to moment.js that formats dates in a short format like it's used on ResearchGate.

License: MIT License

JavaScript 100.00%

moment-shortformat's Introduction

Short Date Formatter Build Status npm

moment-shortformat.js is an extension to moment.js that formats dates in a short format like it's used on ResearchGate.

Absolute time difference output of moment.short()
0 to 59 seconds 1s to 59s
1 to 59 minutes 1m to 59m
1 to 23 hours 1h to 23h
1 to 6 days 1d to 6d
>= 7 days but same year e.g Sep 3 or Feb 28
>= 7 days but different year e.g Sep 3, 2023 or Feb 28, 1989

It works for both negative and positive time differences.

Usage

Works with AMD, CommonJS and browser environments.

// AMD
define(['moment-shortformat'], function(moment) { moment().short() });
// CommonJS
var moment = require('moment-shortformat');
moment().short()
<!-- Browser -->
<script type="text/javascript" src="/moment.js"></script>
<script type="text/javascript" src="/moment-shortformat.js"></script>
<script type="text/javascript">
    moment().short();
</script>

Works on both past and future dates.

Methods

moment().short(Boolean, now)

Short formatting the time difference and prefixing or suffixing the formatted time by using moment.js relativeTime templates future and past. (default in %s and %s ago)

If the boolean parameter is set to true the date format will not be prefixed or suffixed.

If the now parameter is set to a moment() it will use that as the "now" difference

Examples:

Formats time relative to current time.

moment(moment() + (36e5 * 5)).short() // in 5h
moment(moment() - (36e5 * 5)).short() // 5h ago
moment(moment() + (36e5 * 5)).short(true) // 5h
moment(moment() - (36e5 * 5)).short(true) // 5h

Times greater than 1 week are converted to dates like Mar 7, or if the year of the date does not match the current year it is convert to Mar 7, 2031

moment(moment() + 6048e5).short()
// Mar 7

Using a different "now"

moment(moment() + (36e5 * 5)).short(false, moment() + (36e5 * 3)) // in 2h

moment-shortformat's People

Contributors

danez avatar greenkeeper[bot] avatar blanchg avatar

Watchers

whatasoda 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.