refactor datetime function