├── README.md ├── license.txt ├── package.json ├── test.js └── timeago.js /README.md: -------------------------------------------------------------------------------- 1 | timeago 2 | ======= 3 | 4 | Simple timeago function takes same params as 'new Date(...)' 5 | 6 | ```` 7 | var ta = require('./time-ago.js') // node.js 8 | var ta = timeago(); // browser 9 | ```` 10 | 11 | ###ta.ago(..., [short]) 12 | ```` 13 | ta.ago(new Date()-1000); => "1 second ago" 14 | ta.ago(new Date()-2000); => "2 seconds ago" 15 | 16 | ta.ago(1); => "44 years ago" 17 | 18 | // takes twitter's created_at date format, in your timezone 19 | ta.ago('Sun Jun 28 19:44:05 +0000 2013'); => "2 days ago" 20 | 21 | // and UTC 22 | ta.ago('1997-07-16T19:20+01:00'); => "16 years ago" 23 | 24 | // with optional short parameter 25 | ta.ago(new Date()-1000, true); => "1s" 26 | ta.ago(new Date()-1000 * 60, true); => "1m" 27 | ta.ago(new Date()-1000 * 60 * 60, true); => "1h" 28 | 29 | ```` 30 | ###ta.today() 31 | ```` 32 | ta.today() function shows Day, Month, Date, Yr 33 | ==> 'Monday, June 1, 1970' 34 | ```` 35 | 36 | ###ta.timefriendly('x period') 37 | ```` 38 | ta.timefriendly('1 hour') // convert to ms: seconds, minutes, hours, days, weeks, months, years 39 | ==> 3600000 40 | 41 | ta.timefriendly('1 hour') // convert to ms: seconds, minutes, hours, days, weeks, months, years 42 | ==> 3600000 43 | ```` 44 | 45 | ###ta.mintoread(text, [altcmt, wpm]) 46 | Cool Medium like 'x min to read' feature 47 | ```` 48 | ta.mintoread('six hundred words of text') // calculate based on 200 wpm reading speed 49 | ==> "3 min to read" 50 | 51 | ta.mintoread('six hundred words of text', ' minutes to finish') // optional alternate comment 52 | ==> "3 minutes to finish" 53 | 54 | ta.mintoread('six hundred words of text', null, 300) // alternate wpm 55 | ==> "2 min to read" 56 | ```` 57 | -------------------------------------------------------------------------------- /license.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Chris Borkert 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "time-ago", 3 | "description": "", 4 | "url": "http://github.com/dpweb/time-ago", 5 | "keywords": [ 6 | "util", 7 | "nodejs" 8 | ], 9 | "author": { 10 | "name": "C Borkert", 11 | "email": "ILIKECHEESE@dpsw.info" 12 | }, 13 | "main": "timeago.js", 14 | "dependencies": { 15 | }, 16 | "repository": { 17 | "type": "git", 18 | "url": "git://github.com/dpweb/time-ago.git" 19 | }, 20 | "version": "0.2.1" 21 | } 22 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var ta = require('./timeago.js') 2 | 3 | console.log( 4 | ta.ago(new Date() - 1000), ta.ago(new Date() - 1000) === '1 second ago', 5 | ta.ago(new Date() - 60000 * 180), ta.ago(new Date() - 60000 * 180) === '3 hours ago', 6 | new Date(1), ta.ago(new Date(1)), ta.ago(new Date(1)) === '47 years ago' 7 | ); 8 | 9 | console.log( 10 | ta.ago(new Date() - 1000, true), ta.ago(new Date() - 1000, true) === '1s', 11 | ta.ago(new Date() - 60000 * 180, true), ta.ago(new Date() - 60000 * 180, true) === '3h', 12 | new Date(1), ta.ago(new Date(1), true), ta.ago(new Date(1), true) === '47y' 13 | ); 14 | 15 | console.log( 16 | 'Today is ' + ta.today() 17 | ); 18 | 19 | console.log( 20 | ta.timefriendly('1 hour'), ta.timefriendly('1 hour') === 1000 * 60 * 60, 21 | ta.timefriendly('2 days'), ta.timefriendly('2 days') === 1000 * 60 * 60 * 48, 22 | ta.timefriendly('2 weeks'), ta.timefriendly('2 weeks') === 1000 * 60 * 60 * 24 * 14 23 | ); 24 | 25 | var i = 0, 26 | text = ''; 27 | while (i++ < 600) text += 'text '; 28 | console.log(text.length, 'chars, min to read', ta.mintoread(text)); 29 | -------------------------------------------------------------------------------- /timeago.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var timeago = function() { 4 | 5 | var o = { 6 | second: 1000, 7 | minute: 60 * 1000, 8 | hour: 60 * 1000 * 60, 9 | day: 24 * 60 * 1000 * 60, 10 | week: 7 * 24 * 60 * 1000 * 60, 11 | month: 30 * 24 * 60 * 1000 * 60, 12 | year: 365 * 24 * 60 * 1000 * 60 13 | }; 14 | var obj = {}; 15 | 16 | obj.ago = function(nd, s) { 17 | var r = Math.round, 18 | dir = ' ago', 19 | pl = function(v, n) { 20 | return (s === undefined) ? n + ' ' + v + (n > 1 ? 's' : '') + dir : n + v.substring(0, 1) 21 | }, 22 | ts = Date.now() - new Date(nd).getTime(), 23 | ii; 24 | if( ts < 0 ) 25 | { 26 | ts *= -1; 27 | dir = ' from now'; 28 | } 29 | for (var i in o) { 30 | if (r(ts) < o[i]) return pl(ii || 'm', r(ts / (o[ii] || 1))) 31 | ii = i; 32 | } 33 | return pl(i, r(ts / o[i])); 34 | } 35 | 36 | obj.today = function() { 37 | var now = new Date(); 38 | var Weekday = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"); 39 | var Month = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); 40 | return Weekday[now.getDay()] + ", " + Month[now.getMonth()] + " " + now.getDate() + ", " + now.getFullYear(); 41 | } 42 | 43 | obj.timefriendly = function(s) { 44 | var t = s.match(/(\d).([a-z]*?)s?$/); 45 | return t[1] * eval(o[t[2]]); 46 | } 47 | 48 | obj.mintoread = function(text, altcmt, wpm) { 49 | var m = Math.round(text.split(' ').length / (wpm || 200)); 50 | return (m || '< 1') + (altcmt || ' min to read'); 51 | } 52 | 53 | return obj; 54 | } 55 | 56 | if (typeof module !== 'undefined' && module.exports) 57 | module.exports = timeago(); 58 | --------------------------------------------------------------------------------