Note that there are some explanatory texts on larger screens.

plurals
  1. POHow would I return dates via JSON in the specified format?
    primarykey
    data
    text
    <p>If you go into your SO profile and click on 'days visited', you see a handy jQuery datepicker with the days you've visited the site shaded a luscious green. I'm trying my hand at something similar, and started by picking apart SO's implementation. I'm stuck on figuring out how they were able to format and return the visited dates in the specific JSON format they used. Here's a copy of mine (you can find similar by inspecting the source of that page):</p> <p>var visited = {2009:{5:{28:1,29:1},6:{3:1,4:1,5:1,7:1,8:1,9:1,10:1,11:1,12:1,13:1,14:1,15:1,16:1,17:1,19:1,20:1,21:1,22:1,23:1,24:1,25:1,27:1,29:1,30:1},7:{1:1,2:1,3:1,4:1,5:1,6:1,7:1,8:1,9:1,10:1,11:1,12:1,13:1,14:1,15:1,16:1,18:1,20:1,21:1,22:1,23:1,24:1,26:1,27:1,28:1,29:1,30:1,31:1},8:{2:1,3:1,4:1,5:1,6:1,7:1,11:1,12:1,13:1,17:1,20:1,24:1,25:1,27:1,28:1},9:{9:1,10:1,15:1,23:1,24:1},10:{1:1,5:1,7:1,8:1,9:1,12:1,13:1,20:1,21:1,22:1,27:1,28:1,30:1},11:{5:1,9:1,10:1,16:1,17:1,24:1,25:1,26:1,30:1},12:{2:1,7:1,8:1,9:1,11:1,12:1,13:1,14:1,15:1,16:1,17:1,18:1,21:1,22:1,23:1,28:1,30:1}},2010:{1:{5:1,8:1,11:1,12:1,13:1,14:1,15:1,18:1,19:1,20:1,21:1,26:1,27:1,28:1},2:{3:1,10:1,17:1,22:1,23:1,26:1},3:{1:1,2:1,3:1,5:1,8:1},4:{8:1,9:1,13:1,19:1,20:1,21:1,22:1,26:1,28:1,29:1},5:{3:1,4:1,5:1,6:1,7:1,11:1,12:1,17:1,28:1},6:{2:1,4:1,7:1,8:1,9:1,10:1,14:1,15:1,16:1,17:1,18:1,21:1,23:1,25:1},7:{7:1,8:1,9:1,12:1,21:1,22:1,23:1,28:1,29:1},8:{2:1,3:1,4:1,5:1,9:1,10:1,11:1,16:1,17:1,18:1,19:1,20:1,25:1,26:1,29:1,30:1,31:1},9:{1:1,8:1},10:{6:1,11:1,15:1,21:1,26:1,27:1,28:1},11:{3:1,4:1,8:1,11:1,15:1,16:1,17:1,19:1,20:1,21:1,23:1,30:1},12:{1:1,2:1,13:1,14:1,15:1,16:1,17:1,20:1,22:1,23:1,28:1}},2011:{1:{3:1,5:1,6:1,7:1,12:1,14:1,18:1,20:1,21:1,25:1,26:1,27:1,28:1,31:1},2:{1:1,2:1,3:1,6:1,7:1,8:1,9:1,10:1,11:1}}};</p> <p>It's basically an array: [year][month][day]. I'm failing to send back data in that format though and was wondering if anyone (perhaps one of the SO crew) had any insight.</p> <p>Hope no one thinks I'm ripping off SO with this question. I'm truly just curious how they did it.</p> <p>EDIT: To clarify, I'm asking what the controller action <em>might</em> look like if I had a grip of .NET DateTime objects and wanted to return JSON in the format mentioned.</p> <p>My test controller method looks like:</p> <pre><code>[HttpPost] public JsonResult GetDates(int id) { var model = BaseRepository.FindById(id); testDates = new DateTime[] { DateTime.Today, DateTime.Today.AddDays(1) }; return Json(testDates); } </code></pre> <p>And when I call it via AJAX with:</p> <pre><code>$.ajax({ type: 'POST', url: '&lt;%= Url.Action("ClosedDates", "Venue", new { id = Model.Id }) %&gt;', dataType: 'json', success: function (data) { alert(data); } }); </code></pre> <p>The alert shows me the expected list of dates, but in .NET date format. I'm not sure if I should be massaging the data in the controller, or in the callback on the client.</p> <p>EDIT 2</p> <p>Ok, so maybe the use of that array will help make more sense of this. Again, riffing off SO's implementation (note how the beforeShowDay function parses the array for the current date being evaluated):</p> <pre><code>var visited = { 2009: { 5: { 28: 1, 29: 1 }, 6: { 3: 1, 4: 1, 5: 1, 7: 1, 8: 1, 9: 1, 10: 1, 11: 1, 12: 1, 13: 1, 14: 1, 15: 1, 16: 1, 17: 1, 19: 1, 20: 1, 21: 1, 22: 1, 23: 1, 24: 1, 25: 1, 27: 1, 29: 1, 30: 1 }, 7: { 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1, 9: 1, 10: 1, 11: 1, 12: 1, 13: 1, 14: 1, 15: 1, 16: 1, 18: 1, 20: 1, 21: 1, 22: 1, 23: 1, 24: 1, 26: 1, 27: 1, 28: 1, 29: 1, 30: 1, 31: 1 }, 8: { 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 11: 1, 12: 1, 13: 1, 17: 1, 20: 1, 24: 1, 25: 1, 27: 1, 28: 1 }, 9: { 9: 1, 10: 1, 15: 1, 23: 1, 24: 1 }, 10: { 1: 1, 5: 1, 7: 1, 8: 1, 9: 1, 12: 1, 13: 1, 20: 1, 21: 1, 22: 1, 27: 1, 28: 1, 30: 1 }, 11: { 5: 1, 9: 1, 10: 1, 16: 1, 17: 1, 24: 1, 25: 1, 26: 1, 30: 1 }, 12: { 2: 1, 7: 1, 8: 1, 9: 1, 11: 1, 12: 1, 13: 1, 14: 1, 15: 1, 16: 1, 17: 1, 18: 1, 21: 1, 22: 1, 23: 1, 28: 1, 30: 1} }, 2010: { 1: { 5: 1, 8: 1, 11: 1, 12: 1, 13: 1, 14: 1, 15: 1, 18: 1, 19: 1, 20: 1, 21: 1, 26: 1, 27: 1, 28: 1 }, 2: { 3: 1, 10: 1, 17: 1, 22: 1, 23: 1, 26: 1 }, 3: { 1: 1, 2: 1, 3: 1, 5: 1, 8: 1 }, 4: { 8: 1, 9: 1, 13: 1, 19: 1, 20: 1, 21: 1, 22: 1, 26: 1, 28: 1, 29: 1 }, 5: { 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 11: 1, 12: 1, 17: 1, 28: 1 }, 6: { 2: 1, 4: 1, 7: 1, 8: 1, 9: 1, 10: 1, 14: 1, 15: 1, 16: 1, 17: 1, 18: 1, 21: 1, 23: 1, 25: 1 }, 7: { 7: 1, 8: 1, 9: 1, 12: 1, 21: 1, 22: 1, 23: 1, 28: 1, 29: 1 }, 8: { 2: 1, 3: 1, 4: 1, 5: 1, 9: 1, 10: 1, 11: 1, 16: 1, 17: 1, 18: 1, 19: 1, 20: 1, 25: 1, 26: 1, 29: 1, 30: 1, 31: 1 }, 9: { 1: 1, 8: 1 }, 10: { 6: 1, 11: 1, 15: 1, 21: 1, 26: 1, 27: 1, 28: 1 }, 11: { 3: 1, 4: 1, 8: 1, 11: 1, 15: 1, 16: 1, 17: 1, 19: 1, 20: 1, 21: 1, 23: 1, 30: 1 }, 12: { 1: 1, 2: 1, 13: 1, 14: 1, 15: 1, 16: 1, 17: 1, 20: 1, 22: 1, 23: 1, 28: 1} }, 2011: { 1: { 3: 1, 5: 1, 6: 1, 7: 1, 12: 1, 14: 1, 18: 1, 20: 1, 21: 1, 25: 1, 26: 1, 27: 1, 28: 1, 31: 1 }, 2: { 1: 1, 2: 1, 3: 1, 6: 1, 7: 1, 8: 1, 9: 1, 10: 1, 11: 1}} }; $('#calendar').datepicker({ beforeShowDay: function (date) { var y = date.getFullYear(); var m = date.getMonth() + 1; var d = date.getDate(); var hasVisited = visited[y] &amp;&amp; visited[y][m] &amp;&amp; visited[y][m][d]; return [false, (hasVisited ? 'ui-state-active' : ''), (y + '-' + m + '-' + d)]; } }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload