Coder Social home page Coder Social logo

bryant1410 / js_humanized_time_span Goto Github PK

View Code? Open in Web Editor NEW

This project forked from quantumcatgirl/js_humanized_time_span

0.0 1.0 0.0 6 KB

A fully customisable 'time ago in words' implementation for use in the browser

JavaScript 100.00%

js_humanized_time_span's Introduction

humanized_time_span

humanized_time_span returns the difference between two Dates as a nice, human string for use in the browser.

usage

Include the script (it's native JS so doesn't require anything else) and you're ready to go.

Syntax

humanized_time_span(date, ref_date, date_formats, time_units)

Only date is required.

Examples

humanized_time_span("2010/09/10 10:00:00") => "3 days ago" (using now as a reference)

humanized_time_span("2010/09/10 10:00:00", "2010/09/10 12:00:00") => "2 hours ago"

Customisation

Custom date formats can be set as follows:

var custom_date_formats = {
  past: [
    { ceiling: 60, text: "less than a minute ago" },
    { ceiling: 86400, text: "$hours hours, $minutes minutes and $seconds seconds ago" },
    { ceiling: null, text: "$years years ago" }
  ],
  future: [
    { ceiling: 60, text: "in less than a minute" },
    { ceiling: 86400, text: "in $hours hours, $minutes minutes and $seconds seconds time" },
    { ceiling: null, text: "in $years years" }
  ]
}

humanized_time_span("2010/09/10 10:00:00", "2010/09/10 10:00:05", custom_date_formats) 
  => "less than a minute ago"
humanized_time_span("2010/09/10 10:00:00", "2010/09/10 17:01:25", custom_date_formats) 
  => "5 hours, 1 minute and 25 seconds ago"
humanized_time_span("2010/09/10 10:00:00", "2012/09/10 10:00:00", custom_date_formats) 
  => "in 2 years"

Here the date format's ceiling is in seconds. Formats are walked through until one is reached where the ceiling is more than the difference between the two times or is null.

Please note that the last date format provided must not have a ceiling.

Variables in the format text should be prefixed with a $. eg. $xxx where xxx is the name of the time unit. The text should always be written in the plural (eg. "$years years ago") and the text will be automatically de-pluralized if, say in this case, there is only 1 year.

For those who live by different time rules, time_units can also be customised:

var custom_date_formats = [
  { ceiling: null, text: "$moggles moggles, $tocks tocks and $ticks ticks ago" },
]

var custom_time_units = [
  [20, 'moggles'],
  [10, 'tocks'],
  [1, 'ticks']
]
time_ago("2010/01/01 00:00:00", "2010/01/01 00:00:53", custom_date_formats, custom_time_units) 
  => "2 moggles, 1 tock and 3 ticks ago"

Custom date formats will have to be used when using custom time_units.

the default time units are:

[31556926, 'years'],
[2629744, 'months'],
[86400, 'days'],
[3600, 'hours'],
[60, 'minutes'],
[1, 'seconds']

js_humanized_time_span's People

Contributors

bryant1410 avatar obazoud avatar

Watchers

 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.