├── .gitignore ├── .travis.yml ├── README.md ├── bower.json ├── neatime.js ├── package.json └── tests.js /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | /npm-debug.log 3 | .DS_Store 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | before_script: 3 | - npm install -g grunt-cli 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | neatime [![npm]](http://npm.im/neatime) [![bower]](http://bower.io/search/?q=neatime) [![travis]](https://travis-ci.org/developit/neatime) 2 | ======= 3 | 4 | Returns a simple relative time string. 5 | 6 | 7 | Usage Examples 8 | -------------- 9 | 10 | 11 | ```js 12 | // within the last minute 13 | neatime(new Date()-1e4) == 'Just now' 14 | 15 | // within the last hour 16 | neatime(Date.now()-1e6) == '16m' 17 | 18 | // within the last hour 19 | neatime(Date.now()-1e7) == '2h' 20 | 21 | // within the last 7 days 22 | neatime('Dec 31, 2014') == '4d' 23 | 24 | // over 7 days ago gives a simple date 25 | neatime('July 20, 2015') == 'Jul 20' 26 | 27 | // over half a year ago adds the year 28 | neatime('July 4, 2014') == 'Jul 4, 2014' 29 | ``` 30 | 31 | 32 | License 33 | ------- 34 | 35 | WTFPL 36 | 37 | 38 | [npm]: https://img.shields.io/npm/v/neatime.svg 39 | [bower]: https://img.shields.io/bower/v/neatime.svg 40 | [travis]: https://img.shields.io/travis/developit/neatime.svg 41 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "neatime", 3 | "main": "neatime.js", 4 | "version": "1.0.0", 5 | "homepage": "https://github.com/developit/neatime", 6 | "authors": [ 7 | "Jason Miller " 8 | ], 9 | "description": "Returns a simple relative time string", 10 | "moduleType": [ 11 | "node" 12 | ], 13 | "keywords": [ 14 | "time", 15 | "date", 16 | "moment", 17 | "since", 18 | "before" 19 | ], 20 | "license": "WTFPL", 21 | "ignore": [ 22 | "**/.*", 23 | "node_modules", 24 | "bower_components", 25 | "test", 26 | "tests" 27 | ] 28 | } 29 | -------------------------------------------------------------------------------- /neatime.js: -------------------------------------------------------------------------------- 1 | /** Returns a simple relative time string 2 | * @example 3 | * neatime(new Date()-1e4) == 'Just now' // within the last minute 4 | * neatime(Date.now()-1e6) == '16m' // within the last hour 5 | * neatime(Date.now()-1e7) == '2h' // within the last hour 6 | * neatime('Dec 31, 2014') == '4d' // within the last 7 days 7 | * neatime('Dec 25, 2014') == 'Dec 25' // over 7 days ago gives a simple date 8 | * neatime('July 4, 2014') == 'Jul 4, 2014' // over half a year ago adds the year 9 | */ 10 | function neatime(text) { 11 | var now = new Date(), 12 | date = new Date(text), 13 | diff = (now.getTime() - date.getTime()) / 1000; 14 | if (diff <= 60) { 15 | text = 'Just now'; 16 | } 17 | else if ( (diff /= 60) < 60 ) { 18 | text = (diff|0) + 'm'; 19 | } 20 | else if ( (diff /= 60) < 24 ) { 21 | text = (diff|0) + 'h'; 22 | } 23 | else if ( (diff /= 24) < 7 ) { 24 | text = (diff|0) + 'd'; 25 | } 26 | else { 27 | text = String(date).split(' ')[1] + ' ' + date.getDate(); 28 | if (diff>182 && now.getYear()!==date.getYear()) { 29 | text += ', ' + date.getFullYear(); 30 | } 31 | } 32 | return text; 33 | } 34 | neatime.neatime = neatime; 35 | 36 | module.exports = neatime; 37 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "neatime", 3 | "version": "1.0.0", 4 | "description": "Returns a simple relative time string", 5 | "main": "neatime.js", 6 | "scripts": { 7 | "test": "node tests.js" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/developit/neatime.git" 12 | }, 13 | "keywords": [ 14 | "time", 15 | "date", 16 | "moment", 17 | "since", 18 | "before" 19 | ], 20 | "author": "Jason Miller ", 21 | "license": "WTFPL", 22 | "bugs": { 23 | "url": "https://github.com/developit/neatime/issues" 24 | }, 25 | "homepage": "https://github.com/developit/neatime" 26 | } 27 | -------------------------------------------------------------------------------- /tests.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | neatime = require('./'), 3 | MIN = 60000, 4 | HOUR = MIN*60, 5 | DAY = HOUR*24; 6 | 7 | // within the last minute 8 | assert.equal( 9 | neatime(new Date()-1e4), 10 | 'Just now' 11 | ); 12 | 13 | // within the last hour 14 | assert.equal( 15 | neatime(Date.now() - 16*MIN), 16 | '16m' 17 | ); 18 | 19 | // within the last hour 20 | assert.equal( 21 | neatime(Date.now() - 2*HOUR), 22 | '2h' 23 | ); 24 | 25 | // within the last 7 days 26 | assert.equal( 27 | neatime(Date.now() - 4*DAY), 28 | '4d' 29 | ); 30 | 31 | // over 7 days ago gives a simple date 32 | var d = new Date(); 33 | d.setMonth(d.getMonth()-1); 34 | var m = d.toDateString().split(' ')[1] 35 | assert.equal( 36 | neatime(m+' 1, '+d.getFullYear()), 37 | m+' 1' 38 | ); 39 | 40 | // over half a year ago adds the year 41 | d.setFullYear(d.getFullYear()-1); 42 | assert.equal( 43 | neatime(m+' 1, '+d.getFullYear()), 44 | m+' 1, '+d.getFullYear() 45 | ); 46 | 47 | console.log('All tests passed'); 48 | process.exit(0); 49 | --------------------------------------------------------------------------------