Created
March 2, 2015 15:10
-
-
Save matthewbednarski/b88eaa941796f7423b50 to your computer and use it in GitHub Desktop.
Moment parametrized date range An example of a parametrized callback for filtering by date range. // source http://jsbin.com/venape
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="description" content="An example of a parametrized callback for filtering by date range."> | |
<script src="https://rawgit.com/lodash/lodash/3.0.1/lodash.min.js"></script> | |
<script src="http://momentjs.com/downloads/moment.min.js"></script> | |
<meta charset="utf-8"> | |
<title>Moment parametrized date range</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
var b = moment(); | |
var a = b.subtract(1, 'hours'); | |
var c = b.add(1, 'hours'); | |
var d = moment(); | |
var day1 = moment.unix(1318781800); | |
var day2 = moment.unix(1318781876); | |
var day3 = moment.unix(1318781880); | |
var filter = function(itemDate, min, max, options) { | |
var mItem = moment(itemDate); | |
var mMin = moment(min); | |
var mMax = moment(max); | |
printDate(mItem); | |
printDate(mMin); | |
printDate(mMax); | |
if (options === undefined) { | |
options = {}; | |
} | |
if (options.granularity === undefined) { | |
options.granularity = 'minute'; | |
} | |
if (options.includeMin === undefined) { | |
options.includeMin = true; | |
} | |
if (options.includeMax === undefined) { | |
options.includeMax = false; | |
} | |
var isSameMin; | |
var isSameMax; | |
var isBetween = mItem.isBetween(mMin, mMax, options.granularity); | |
if (options.includeMin && options.includeMax) { | |
isSameMin = mItem.isSame(mMin, options.granularity); | |
isSameMax = mItem.isSame(mMax, options.granularity); | |
return isBetween || isSameMin || isSameMax; | |
} else if (options.includeMin) { | |
isSameMin = mItem.isSame(mMin, options.granularity); | |
return isBetween || isSameMin; | |
} else if (options.includeMax) { | |
isSameMax = mItem.isSame(mMax, options.granularity); | |
return isBetween || isSameMax; | |
} else { | |
return isBetween; | |
} | |
}; | |
var printDate = function(mDate) { | |
console.log(mDate.toISOString()); | |
}; | |
console.log(filter(day2, day1, day3)); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">var b = moment(); | |
var a = b.subtract(1, 'hours'); | |
var c = b.add(1, 'hours'); | |
var d = moment(); | |
var day1 = moment.unix(1318781800); | |
var day2 = moment.unix(1318781876); | |
var day3 = moment.unix(1318781880); | |
var filter = function(itemDate, min, max, options) { | |
var mItem = moment(itemDate); | |
var mMin = moment(min); | |
var mMax = moment(max); | |
printDate(mItem); | |
printDate(mMin); | |
printDate(mMax); | |
if (options === undefined) { | |
options = {}; | |
} | |
if (options.granularity === undefined) { | |
options.granularity = 'minute'; | |
} | |
if (options.includeMin === undefined) { | |
options.includeMin = true; | |
} | |
if (options.includeMax === undefined) { | |
options.includeMax = false; | |
} | |
var isSameMin; | |
var isSameMax; | |
var isBetween = mItem.isBetween(mMin, mMax, options.granularity); | |
if (options.includeMin && options.includeMax) { | |
isSameMin = mItem.isSame(mMin, options.granularity); | |
isSameMax = mItem.isSame(mMax, options.granularity); | |
return isBetween || isSameMin || isSameMax; | |
} else if (options.includeMin) { | |
isSameMin = mItem.isSame(mMin, options.granularity); | |
return isBetween || isSameMin; | |
} else if (options.includeMax) { | |
isSameMax = mItem.isSame(mMax, options.granularity); | |
return isBetween || isSameMax; | |
} else { | |
return isBetween; | |
} | |
}; | |
var printDate = function(mDate) { | |
console.log(mDate.toISOString()); | |
}; | |
console.log(filter(day2, day1, day3));</script></body> | |
</html> |
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
var b = moment(); | |
var a = b.subtract(1, 'hours'); | |
var c = b.add(1, 'hours'); | |
var d = moment(); | |
var day1 = moment.unix(1318781800); | |
var day2 = moment.unix(1318781876); | |
var day3 = moment.unix(1318781880); | |
var filter = function(itemDate, min, max, options) { | |
var mItem = moment(itemDate); | |
var mMin = moment(min); | |
var mMax = moment(max); | |
printDate(mItem); | |
printDate(mMin); | |
printDate(mMax); | |
if (options === undefined) { | |
options = {}; | |
} | |
if (options.granularity === undefined) { | |
options.granularity = 'minute'; | |
} | |
if (options.includeMin === undefined) { | |
options.includeMin = true; | |
} | |
if (options.includeMax === undefined) { | |
options.includeMax = false; | |
} | |
var isSameMin; | |
var isSameMax; | |
var isBetween = mItem.isBetween(mMin, mMax, options.granularity); | |
if (options.includeMin && options.includeMax) { | |
isSameMin = mItem.isSame(mMin, options.granularity); | |
isSameMax = mItem.isSame(mMax, options.granularity); | |
return isBetween || isSameMin || isSameMax; | |
} else if (options.includeMin) { | |
isSameMin = mItem.isSame(mMin, options.granularity); | |
return isBetween || isSameMin; | |
} else if (options.includeMax) { | |
isSameMax = mItem.isSame(mMax, options.granularity); | |
return isBetween || isSameMax; | |
} else { | |
return isBetween; | |
} | |
}; | |
var printDate = function(mDate) { | |
console.log(mDate.toISOString()); | |
}; | |
console.log(filter(day2, day1, day3)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment