-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added UMD & bower support, bumped version
- Loading branch information
Showing
5 changed files
with
81 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,3 +26,5 @@ node_modules | |
|
||
# Users Environment Variables | ||
.lock-wscript | ||
|
||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "round-date", | ||
"main": "roundDate.js", | ||
"version": "1.0.1", | ||
"homepage": "https://github.com/atruskie/round-date", | ||
"description": "A small module for rounding dates to arbitrary intervals", | ||
"moduleType": [ | ||
"amd", | ||
"globals", | ||
"node" | ||
], | ||
"keywords": [ | ||
"round", | ||
"date" | ||
], | ||
"license": "MIT", | ||
"ignore": [ | ||
"**/.*", | ||
"node_modules", | ||
"bower_components", | ||
"test", | ||
"tests" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
(function (root, factory) { | ||
if (typeof define === 'function' && define.amd) { | ||
// AMD. Register as an anonymous module. | ||
define([], factory); | ||
} else if (typeof exports === 'object') { | ||
// Node. Does not work with strict CommonJS, but | ||
// only CommonJS-like environments that support module.exports, | ||
// like Node. | ||
module.exports = factory(); | ||
} else { | ||
// Browser globals (root is window) | ||
root.returnExports = factory(); | ||
} | ||
}(this, function () { | ||
|
||
function roundDate(roundStyle, roundToSeconds, date) { | ||
if (arguments.length !== 3) { | ||
throw new Error("Expected 2 arguments"); | ||
} | ||
|
||
var startOfDay = new Date(date).setHours(0, 0, 0), | ||
roundToMilliseconds = roundToSeconds * 1000, | ||
msFromMidnight = (+date) - startOfDay, | ||
remainder = msFromMidnight % roundToMilliseconds, | ||
result = msFromMidnight; | ||
|
||
if (remainder !== 0) { | ||
if (roundStyle === "round") { | ||
var fraction = (msFromMidnight / roundToMilliseconds) % 1; | ||
roundStyle = fraction >= 0.5 ? "ceil" : "floor"; | ||
} | ||
|
||
if (roundStyle === "floor") { | ||
result -= remainder | ||
} | ||
else if (roundStyle === "ceil") { | ||
result += (roundToMilliseconds - remainder); | ||
} | ||
else { | ||
return; | ||
} | ||
} | ||
|
||
return new Date(startOfDay + result); | ||
} | ||
|
||
return { | ||
round: roundDate.bind(null, "round"), | ||
floor: roundDate.bind(null, "floor"), | ||
ceil: roundDate.bind(null, "ceil") | ||
}; | ||
})); | ||
|