Created
August 14, 2014 13:21
-
-
Save NearlyUnique/30f7a171a3a89121a542 to your computer and use it in GitHub Desktop.
create an ISODate for mongo queries, usage aDate : { $gt:queryDate({hour:-4}), $lt:queryDate() }
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 queryDate = function (diff) { | |
var d = new Date(), p = function(num){return num<10?"0"+num:num+"";}; | |
diff = diff || {}; | |
d.setUTCFullYear(d.getUTCFullYear() + (diff.year||0)); | |
d.setUTCMonth(d.getUTCMonth() + (diff.month||0)); | |
d.setUTCDate(d.getUTCDate() + (diff.day||0)); | |
d.setUTCHours(d.getUTCHours() + (diff.hour||0)); | |
d.setUTCMinutes(d.getUTCMinutes() + (diff.min||0)); | |
return ISODate(d.getUTCFullYear() + "-" + | |
p(d.getUTCMonth()+1) + "-" + | |
p(d.getUTCDate()) + "T" + | |
p(d.getUTCHours()) + ":" + | |
p(d.getUTCMinutes()) + ":00.000Z"); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment