Refactoring datepicker code