Giter Club home page Giter Club logo

timeago-simple's Introduction

timeago-simple

npm npm npm Travis Coveralls

Timeago is a simple plugin that makes it easy to support automatically updating fuzzy timestamps (e.g. "4 minutes ago" or "1 day ago")

NPM

NodeJS Plugin

Install:

npm install timeago-simple --save

Usage:

var timeago = require("timeago-simple");


var timeStamp = 'Tue Mar 21 2017 15:08:46 GMT+0000 (UTC)';

var newTime = timeago.simple(timeStamp);

// 9 hours ago


var futureTimeStamp = 'Tue Mar 21 2019 15:08:46 GMT+0000 (UTC)';

var newFutureTime = timeago.simple(futureTimeStamp);

// in 2 years

Bower (plain JavaScript)

Add via Bower:

bower install timeago-simple --save

Add via CDN:

<!--Full version-->
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/timeago.js" async></script>

<!--Minified version-->
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/timeago.min.js" async></script>

Directory for legasy versions

https://cdn.jsdelivr.net/npm/[email protected]/

Usage:

<p>Posted: <span class="timeago">Tue Mar 21 2017 15:08:46 GMT+0000 (UTC)</span></p>

<p>Posted: <span class="timeago">Tue Apr 2 2017</span></p>

<p>Posted: <span class="timeago">12/31/2004</span></p>

<p>Posted: <span class="timeago">2017/03/31</span></p>

<script type="text/javascript" src="/dist/timeago.min.js" async></script>

<!-- Output:
Posted: 12 days ago

Posted: 16 hours ago

Posted: 13 years ago

Posted: 8 days ago
-->


<p>Will be ready <span class="timeago">Tue Mar 21 2020 15:08:46 GMT+0000 (UTC)</span></p>

<p>Will be ready <span class="timeago">06/31/2017</span></p>

<script type="text/javascript" src="/dist/timeago.min.js" async></script>

<!-- Output:

Will be ready in 3 years

Will be ready in 2 month

-->

Troubleshooting:

If we unable to parse the date and time error message will throw in console and original data will display

<p>Posted: <span class="timeago">31/12/2004</span></p>

<p>Posted: <span class="timeago">4366325635463456</span></p>

<p>Posted: <span class="timeago">zcbdsdewea</span></p>

<script type="text/javascript" src="/dist/timeago.min.js" async></script>

<!-- Output:

Posted: 31/12/2004

Posted: 4366325635463456

Posted: zcbdsdewea

-->

<!-- Console Output:

timeago-simple: Please check date and time format! Unable to parse the date & time: 31/12/2004

timeago-simple: Please check date and time format! Unable to parse the date & time: 4366325635463456

timeago-simple: Please check date and time format! Unable to parse the date & time: zcbdsdewea

-->

Tests

npm run cover

Contributing

In lieu of a formal style guide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code.

Release Notes

v.1.2.4

  • Merged past and future time calculation.
  • Depricated future calculation, use only simple function call.

v.1.2.3

  • Add CDN links

v.1.2.2

  • Add functionality for "future" date calculation.

v.1.1.1

  • Add troubleshooting for wrong date format.

v.1.1.2

  • Add seconds display functionality.

timeago-simple's People

Contributors

mikepenzin avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

timeago-simple's Issues

Off-by-one errors and Incorrect pluralization

$ node
> t = require('timeago-simple')
timeago {}
> new Date().toString()
'Mon Aug 22 2022 12:40:27 GMT-0700 (Pacific Daylight Time)'
> t.simple('Mon Aug 22 2021 13:42:27 GMT-0700')
'2 years ago'
  -- Should be '1 year ago'
> t.simple('Mon June 22 2022 12:37:58 GMT-0700')
'2 month ago'
  -- Should be '2 months ago'

The first error isn't even a rounding error, because I ran it an hour in the future. I'm not sure how 0 years, 11 months, 30 days, 23 hours is interpreted as 2 years, but I know date arithmetic is harder than you'd expect something most of us learned in first grade would be.

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.