SunriseSunset Test
A test of Preston Hunt's SunriseSunset javascript class.
by jamstooks
HTML
<script src="http://pastebin.com/raw.php?i=dcnWmHX2&dummy=.js"></script>
<label for='date'>Date</label><br/>
<input type='text' id='date' value='2013-07-2' /><br/>
<label for='lat'>Latitude</label><br/>
<input type='text' id='lat' value='41.49' /><br/>
<label for='lng'>Longitude</label><br/>
<input type='text' id='lng' value='-71.31333' /><br/>
<label for='lng'>Local Timezone Offset</label><br/>
<input type='text' id='offset' value='-4' /><br/>
<label for='zenith'>Zenith</label><br/>
<select id='zenith'>
<option value='90'>Official</option>
<option value='96'>Civil</option>
<option value='102'>Nautical</option>
<option value='108'>Astronomical</option>
</select>
<br/>
<button type='button' onclick='calculateSunriseSunset();'>Caclulate</button>
<div style='border: 1px solid black; margin: 10px; padding: 10px;' id='resultsDiv'></div>
JavaScript
var format = d3.time.format("%Y-%m-%d");
var calculateSunriseSunset = function () {
var d = format.parse(d3.select("#date").attr("value"));
var lat = parseFloat(d3.select("#lat").attr("value"));
var lng = parseFloat(d3.select("#lng").attr("value"));
var zSelect = d3.select("#zenith").node();
var z = parseInt(zSelect.options[zSelect.selectedIndex].value);
// local offset could be calculated with the Google TimeZone API or others
var o = parseInt(d3.select("#offset").attr("value"));
var newport = new SunriseSunset(d.getFullYear(), d.getMonth() + 1, d.getDate(), lat, lng, z);
var div = d3.select("#resultsDiv");
var rise = Math.floor(newport.sunriseLocalHours(o)) + ":" + Math.floor(newport.sunriseLocalHours(o) % 1 * 60);
var set = Math.floor(newport.sunsetLocalHours(o)) + ":" + Math.floor(newport.sunsetLocalHours(o) % 1 * 60);
div.html("rise: " + rise + "<br/>set: " + set);
};