var tArr = [],times = {
"1367999303810": { // today at 9am something
"item1": "val1",
"item2": "val2"
},
"1368000000000": { // today at 10am - 380,000 minutes since EPOCH :)
"item1": "val3",
"item2": "val4"
}
}
function getNearest(findTime) {
for (var i=0;i<tArr.length;i++) {
window.console && console.log(new Date(findTime),new Date(tArr[i]))
if (findTime === tArr[i]) return findTime; // exact match
if (findTime >= tArr[i]) break; // pinpoint
}
if (i==tArr.length) return -1; // nothing found
if (i==tArr.length-1) return tArr[i]; // found the end
// here the time to find is greater than one of the array entries
var diff1 = Math.abs (findTime - tArr[i]), // distance from last entry
diff2 = Math.abs (tArr[i+1] - findTime); // distance to next entry
return diff1 === diff2 ? tArr[i+1] : // return the newest time
diff2 > diff1 ? tArr[i] : tArr[i+1]; // or the nearest
}
for (var t in times) tArr.push(parseInt(t));
tArr.sort();
var findTime = new Date(2013,4,8,9,50,0).getTime();
var time = getNearest(findTime);
if (time !=-1) window.console && console.log(times[time])
var findTime = new Date(2013,4,8,9,58,0).getTime();
var time = getNearest(findTime);
if (time !=-1) window.console && console.log(times[time])
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.