var li = $("#dates a.selected").parent();
var selectedYear = li.attr('id');
Or if you don't need the li object:
var selectedYear = $("#dates a.selected").parent().attr('id')
Update
I think you need to create a function to get the selected year:
function getSelectedYear() {
return $("#dates a.selected").parent().attr('id');
}
Now you can use this in both your click and and page load functions:
$('#myButton').click(function() {
var selectedYear = getSelectedYear();
// ...
});
$(function() {
var selectedYear = getSelectedYear();
// ...
});