• Stars
    star
    150
  • Rank 246,366 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 10 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A moment.js plugin to display human-readable date/time ranges

moment-precise-range

This is a plugin for the moment.js JavaScript library, to display date/time ranges precisely, in a human-readable format.

Moment already contains some support for formatting date/time ranges, however it performs a lot of 'rounding' on the result and yields only an approximate description.

In the example below the difference between the 2 dates is 1 month, 2 days, 3 hours, 4 minutes and 5 seconds exactly, however this is simplified to just 'a month' by the library.

var m1 = moment('2014-01-01 12:00:00','YYYY-MM-DD HH:mm:ss');
var m2 = moment('2014-02-03 15:04:05','YYYY-MM-DD HH:mm:ss');
var diff = moment.duration(m1.diff(m2)).humanize(); // 'a month'

Using the plugin, we can display the exact difference using the same 2 dates:

var m1 = moment('2014-01-01 12:00:00','YYYY-MM-DD HH:mm:ss');
var m2 = moment('2014-02-03 15:04:05','YYYY-MM-DD HH:mm:ss');
var diff = moment.preciseDiff(m1, m2); // '1 month 2 days 3 hours 4 minutes 5 seconds'

To obtain the numeric values rather than a string, pass the value true as the third argument to the method:

var diff = moment.preciseDiff(m1, m2, true); // {years : 0, months : 1, days : 2, hours : 3, minutes : 4, seconds : 5}

Usage

HTML/Browser

To use the plugin in a web page, add a <script> tag referencing the moment-precise-range.js file, ensuring that the tag appears after the tag used to include the moment.js library:

<script src="/scripts/moment.js"></script>
<script src="/scripts/moment-precise-range.js"></script>

Node.js

To use the plugin within a node.js application, add the following require statement into your code:

require('moment-precise-range-plugin');

You can try out the Node package online at tonicdev

More Repositories

1

mosaic

Python script for creating photomosaic images
Python
531
star
2

homoglyph

A big list of homoglyphs and some code to detect them
JavaScript
514
star
3

image_augmentor

Data augmentation tool for images
Python
440
star
4

bitmeteros

BitMeter OS - a cross-platform bandwidth monitor
C
330
star
5

reading-list-mover

A Python utility for moving bookmarks/reading lists between services
Python
200
star
6

markov-text

Python utility that uses a Markov Chain to generate random sentences using a source text
Python
185
star
7

bayesian-classifier

A Naive Bayesian Classifier written in Python
Python
102
star
8

star-charts

Generate SVG star charts using Python
Python
100
star
9

mazes

JavaScript Maze Generator
JavaScript
64
star
10

monkeyshine

A collection of slightly evil JavaScript
JavaScript
45
star
11

lunar-calendar

Generates an HTML Lunar Calendar
HTML
42
star
12

old-time-radio

An internet radio station streaming classic shows from the Golden Age of Radio
JavaScript
34
star
13

convnet-designer

A utility for designing Convolutional Neural Networks
JavaScript
27
star
14

readable-regex

Java library for creating readable regular expressions
Java
26
star
15

javabean-tester

JavaBean Tester
Java
24
star
16

js-planet-phase

A small JavaScript library for rendering realistic moon and planet phases in HTML
JavaScript
22
star
17

table-sorter

Table Sorter
JavaScript
21
star
18

clipper

Page Clipper Bookmarklet
JavaScript
21
star
19

generative-patterns

Web-Based Generative Pattern Maker
JavaScript
19
star
20

wordvis

This is a Python script to generate Sunburst Charts that visualise the structure of English words.
Python
16
star
21

process-roulette

A shell script game where you kill random processes on your computer, the more you kill the higher your score!
Shell
14
star
22

sarsa-lambda

A Python implementation of the SARSA λ reinforcement learning algorithm
Python
11
star
23

regex_parser

A regular expression parser written in JavaScript
JavaScript
10
star
24

https-certificate-expiry-checker

A Python script for checking when HTTPS certificates will expire
Python
10
star
25

star-rise-and-set-times

A browser-based tool for calculating the rising and setting times of stars
JavaScript
9
star
26

connect4

Python
8
star
27

blockchain

Minimum Viable Blockchain in Python
Python
8
star
28

maze.js

Maze Generation Algorithms and Rendering Code
JavaScript
8
star
29

save-restore

Save/Restore Bookmarklet
JavaScript
8
star
30

boggle

A boggle solver and game
Python
7
star
31

bokeh

Bokeh Animation with JavaScript
JavaScript
7
star
32

video-barcode-generator

Video Barcode Generator
Shell
6
star
33

show-passwords

Show Passwords Bookmarklet
HTML
6
star
34

scheme-interpreter

An interpreter for a basic subset of the Scheme programming language
Python
6
star
35

algorithms

Python
5
star
36

solar-system-moons

Python code for generating infographic posters that visualise data about the outer planets in our solar system
Python
5
star
37

top-down-parser

A simple top-down parser written in JavaScript
JavaScript
4
star
38

magnetic-pendulum

An interactive simulation of a Magnetic Pendulum
JavaScript
4
star
39

planetary-systems

Python code to generate an SVG showing planetary systems
Python
4
star
40

stellar-classification-parser

A parser for star classification codes
JavaScript
4
star
41

bitmeteros-python-client

A graphical client for BitMeter OS
Python
4
star
42

zen.sh

A shell script meditation timer for macOS
Shell
3
star
43

netmo

Browser-based remote network monitoring tool
Python
3
star
44

gradient-descent

Python implementations of both Linear and Logistic Regression using Gradient Descent
Python
3
star
45

neural-net

A simple neural network implemented in Python
Python
2
star
46

hltracker

Hotline Tracker
Visual Basic
2
star
47

crypto-tools

Python
1
star
48

twitter-min

Minimalist Twitter Search Client, contained in a single HTML file
HTML
1
star
49

analog-digital-clock

Analogue/Digital Clock
JavaScript
1
star
50

peg-parser

The beginnings of a Parsing Expression Grammar parser
Python
1
star
51

landscape

JavaScript
1
star
52

ball_stand

A 3D model of a ball-stand, useful for displaying the clear acrylic balls often used for contact juggling.
OpenSCAD
1
star
53

reinforcement-robot

Experiment with Reinforcement Learning using robots!
JavaScript
1
star
54

tcpdump-web

A web interface for tcpdump
JavaScript
1
star
55

wordle.sh

A Wordle-solving shell script
Shell
1
star
56

svg-stars

Generate pretty animated SVGs for stars
JavaScript
1
star