Skip to content

Commit

Permalink
cleaning up the rounding on _date.from()
Browse files Browse the repository at this point in the history
Adding documentation for what text would be shown when.
Fixing '1 minutes ago' bug.
Use Math.round instead of Math.floor for the nearest date.
  • Loading branch information
timrwood committed Jul 11, 2011
1 parent dfbd87f commit 4e1a51c
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 23 deletions.
76 changes: 75 additions & 1 deletion README.markdown
Expand Up @@ -5,7 +5,7 @@ Underscore.date is a javascript date library that helps create, manipulate, and

Author: Tim Wood

Version: 0.5.1
Version: 0.5.2

**Note:** There are some api changes that will break your code when upgrading from 0.4.1 to 0.5.0. Read about the changes in the changelog at the bottom of the page.

Expand Down Expand Up @@ -429,6 +429,70 @@ in is later than the first date, and negative if the date passed in is earlier.

The base strings for this function can be customized with `_date.relativeTime`.

The breakdown of which string is displayed when is outlined in the table below.

<table>
<tr>
<th>Range</th>
<th>Key</th>
<th>Sample Output</th>
</tr>
<tr>
<td>0 to 45 seconds</td>
<td>s</td>
<td>seconds ago</td>
</tr>
<tr>
<td>45 to 90 seconds</td>
<td>m</td>
<td>a minute ago</td>
</tr>
<tr>
<td>90 seconds to 45 minutes</td>
<td>mm</td>
<td>2 minutes ago ... 45 minutes ago</td>
</tr>
<tr>
<td>45 to 90 minutes</td>
<td>h</td>
<td>an hour ago</td>
</tr>
<tr>
<td>90 minutes to 22 hours </td>
<td>hh</td>
<td>2 hours ago ... 22 hours ago</td>
</tr>
<tr>
<td>22 to 36 hours</td>
<td>d</td>
<td>a day ago</td>
</tr>
<tr>
<td>36 hours to 25 days</td>
<td>dd</td>
<td>2 days ago ... 25 days ago</td>
</tr>
<tr>
<td>25 to 45 days</td>
<td>M</td>
<td>a month ago</td>
</tr>
<tr>
<td>45 to 345 days</td>
<td>MM</td>
<td>2 months ago ... 11 months ago</td>
</tr>
<tr>
<td>345 to 547 days (1.5 years)</td>
<td>y</td>
<td>a year ago</td>
</tr>
<tr>
<td>548 days+</td>
<td>yy</td>
<td>2 years ago ... 20 years ago</td>
</tr>
</table>


_date.fromNow()
Expand Down Expand Up @@ -537,6 +601,12 @@ Tests

There are a bunch of tests in the test/ folder. Check them out. If you think some tests are missing, open an issue, and I'll add it.

### Unit tests

[Underscore.date unit tests](http://timrwood.github.com/underscore.date/test/test.html)

[Underscore.date customization tests](http://timrwood.github.com/underscore.date/test/customize.html)

### Speed tests
[Floor vs bitwiseor vs bitwisenor vs parseint](http://jsperf.com/floor-vs-bitwise-or-vs-parseint/4)

Expand All @@ -561,6 +631,10 @@ Underscore.date is freely distributable under the terms of the MIT license.
Changelog
=========

### 0.5.2

Buxfix for [issue 8](https://github.com/timrwood/underscore.date/pull/8) and [issue 9](https://github.com/timrwood/underscore.date/pull/9).

### 0.5.1

Buxfix for [issue 5](https://github.com/timrwood/underscore.date/pull/5).
Expand Down
68 changes: 67 additions & 1 deletion index.html
Expand Up @@ -18,7 +18,7 @@ <h1 class="logo">
<p>Underscore.date is a javascript date library that helps create, manipulate, and format dates without extending the Date prototype.</p>

<p>Author: Tim Wood (washwithcare@gmail.com)</p>
<p>Version: 0.5.1</p>
<p>Version: 0.5.2</p>
<p class="filesize">1.82 kb (min + gzip)</p>

<h2>Download</h2>
Expand Down Expand Up @@ -416,7 +416,73 @@ <h2>_date.format()</h2>
_date([2007, 0, 27]).from(_date([2007, 0, 28]), true , true) // 86400000);</code></pre>

<p>The base strings for this function can be customized with <code>_date.relativeTime</code>.</p>

<p>The breakdown of which string is displayed when is outlined in the table below.</p>

<table>
<tr>
<th>Range</th>
<th>Key</th>
<th>Sample Output</th>
</tr>
<tr>
<td>0 to 45 seconds</td>
<td>s</td>
<td>seconds ago</td>
</tr>
<tr>
<td>45 to 90 seconds</td>
<td>m</td>
<td>a minute ago</td>
</tr>
<tr>
<td>90 seconds to 45 minutes</td>
<td>mm</td>
<td>2 minutes ago ... 45 minutes ago</td>
</tr>
<tr>
<td>45 to 90 minutes</td>
<td>h</td>
<td>an hour ago</td>
</tr>
<tr>
<td>90 minutes to 22 hours </td>
<td>hh</td>
<td>2 hours ago ... 22 hours ago</td>
</tr>
<tr>
<td>22 to 36 hours</td>
<td>d</td>
<td>a day ago</td>
</tr>
<tr>
<td>36 hours to 25 days</td>
<td>dd</td>
<td>2 days ago ... 25 days ago</td>
</tr>
<tr>
<td>25 to 45 days</td>
<td>M</td>
<td>a month ago</td>
</tr>
<tr>
<td>45 to 345 days</td>
<td>MM</td>
<td>2 months ago ... 11 months ago</td>
</tr>
<tr>
<td>345 to 547 days (1.5 years)</td>
<td>y</td>
<td>a year ago</td>
</tr>
<tr>
<td>548 days+</td>
<td>yy</td>
<td>2 years ago ... 20 years ago</td>
</tr>
</table>


<h2>_date.fromNow()</h2>

<pre><code>_date.fromNow(withoutSuffix:boolean, asMilliseconds:boolean)</code></pre>
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "underscore.date",
"version": "0.5.1",
"version": "0.5.2",
"description": "Underscore.date is a javascript date library that helps create, manipulate, and format dates without extending the `Date` prototype.",
"homepage": "https://github.com/timrwood/underscore.date",
"author": "Tim Wood <washwithcare@gmail.com> (http://timwoodcreates.com/)",
Expand Down
29 changes: 24 additions & 5 deletions test/date.js
Expand Up @@ -66,17 +66,36 @@ $(function() {
});


test("_date().from() -- suffixless", 11, function() {
test("_date().from() -- suffixless", 30, function() {
var start = _date([2007, 1, 28]);
equal(start.from(_date([2007, 1, 28]).add({s:30}), true), "seconds");
equal(start.from(_date([2007, 1, 28]).add({s:60}), true), "a minute");
equal(start.from(_date([2007, 1, 28]).add({m:5}), true), "5 minutes");
equal(start.from(_date([2007, 1, 28]).add({h:1}), true), "an hour");
equal(start.from(_date([2007, 1, 28]).add({s:44}), true), "seconds");
equal(start.from(_date([2007, 1, 28]).add({s:45}), true), "a minute");
equal(start.from(_date([2007, 1, 28]).add({s:89}), true), "a minute");
equal(start.from(_date([2007, 1, 28]).add({s:90}), true), "2 minutes");
equal(start.from(_date([2007, 1, 28]).add({m:44}), true), "44 minutes");
equal(start.from(_date([2007, 1, 28]).add({m:45}), true), "an hour");
equal(start.from(_date([2007, 1, 28]).add({m:89}), true), "an hour");
equal(start.from(_date([2007, 1, 28]).add({m:90}), true), "2 hours");
equal(start.from(_date([2007, 1, 28]).add({h:5}), true), "5 hours");
equal(start.from(_date([2007, 1, 28]).add({h:21}), true), "21 hours");
equal(start.from(_date([2007, 1, 28]).add({h:22}), true), "a day");
equal(start.from(_date([2007, 1, 28]).add({h:35}), true), "a day");
equal(start.from(_date([2007, 1, 28]).add({h:36}), true), "2 days");
equal(start.from(_date([2007, 1, 28]).add({d:1}), true), "a day");
equal(start.from(_date([2007, 1, 28]).add({d:5}), true), "5 days");
equal(start.from(_date([2007, 1, 28]).add({d:25}), true), "25 days");
equal(start.from(_date([2007, 1, 28]).add({d:26}), true), "a month");
equal(start.from(_date([2007, 1, 28]).add({d:30}), true), "a month");
equal(start.from(_date([2007, 1, 28]).add({d:45}), true), "a month");
equal(start.from(_date([2007, 1, 28]).add({d:46}), true), "2 months");
equal(start.from(_date([2007, 1, 28]).add({d:75}), true), "2 months");
equal(start.from(_date([2007, 1, 28]).add({d:76}), true), "3 months");
equal(start.from(_date([2007, 1, 28]).add({M:1}), true), "a month");
equal(start.from(_date([2007, 1, 28]).add({M:5}), true), "5 months");
equal(start.from(_date([2007, 1, 28]).add({d:344}), true), "11 months");
equal(start.from(_date([2007, 1, 28]).add({d:345}), true), "a year");
equal(start.from(_date([2007, 1, 28]).add({d:547}), true), "a year");
equal(start.from(_date([2007, 1, 28]).add({d:548}), true), "2 years");
equal(start.from(_date([2007, 1, 28]).add({y:1}), true), "a year");
equal(start.from(_date([2007, 1, 28]).add({y:5}), true), "5 years");
});
Expand Down
2 changes: 1 addition & 1 deletion test/test.html
Expand Up @@ -5,7 +5,7 @@
<link rel="stylesheet" href="vendor/qunit.css" type="text/css" media="screen" />
<script type="text/javascript" src="vendor/jquery.js"></script>
<script type="text/javascript" src="vendor/underscore.js"></script>
<script type="text/javascript" src="../underscore.date.js"></script>
<script type="text/javascript" src="../underscore.date.min.js"></script>
<script type="text/javascript" src="vendor/qunit.js"></script>
<script type="text/javascript" src="vendor/jslitmus.js"></script>
<script type="text/javascript" src="date.js"></script>
Expand Down
27 changes: 14 additions & 13 deletions underscore.date.js
Expand Up @@ -3,11 +3,12 @@
// (c) 2011 Tim Wood
// Underscore.date is freely distributable under the terms of the MIT license.
//
// Version 0.5.1
// Version 0.5.2

(function (undefined) {

var _date;
var _date,
round = Math.round;

// left zero fill a number
// see http://jsperf.com/left-zero-filling for performance comparison
Expand Down Expand Up @@ -210,17 +211,17 @@
hours = minutes / 60,
days = hours / 24,
years = days / 365;
return seconds < 45 && substituteTimeAgo('s', ~~ seconds) ||
seconds < 90 && substituteTimeAgo('m') ||
minutes < 45 && substituteTimeAgo('mm', ~~ minutes) ||
minutes < 90 && substituteTimeAgo('h') ||
hours < 24 && substituteTimeAgo('hh', ~~ hours) ||
hours < 48 && substituteTimeAgo('d') ||
days < 25 && substituteTimeAgo('dd', ~~ days) ||
return seconds < 45 && substituteTimeAgo('s', round(seconds)) ||
round(minutes) === 1 && substituteTimeAgo('m') ||
minutes < 45 && substituteTimeAgo('mm', round(minutes)) ||
round(hours) === 1 && substituteTimeAgo('h') ||
hours < 22 && substituteTimeAgo('hh', round(hours)) ||
round(days) === 1 && substituteTimeAgo('d') ||
days < 25 && substituteTimeAgo('dd', round(days)) ||
days < 45 && substituteTimeAgo('M') ||
days < 350 && substituteTimeAgo('MM', ~~ ((days + 15) / 30)) ||
years < 2 && substituteTimeAgo('y') ||
substituteTimeAgo('yy', ~~ years);
days < 345 && substituteTimeAgo('MM', round(days / 30)) ||
round(years) === 1 && substituteTimeAgo('y') ||
substituteTimeAgo('yy', round(years));
}

UnderscoreDate.prototype = {
Expand Down Expand Up @@ -381,7 +382,7 @@
};

// CommonJS module is defined
if (window === undefined && module !== undefined) {
if (typeof window === 'undefined' && typeof module !== 'undefined') {
// Export module
module.exports = _date;
// Integrate with Underscore.js
Expand Down

0 comments on commit 4e1a51c

Please sign in to comment.