Thursday, September 27, 2012

the DateTime.Now of the JavaScript world

The following (in tandem with this posting I wrote on date comparisons and casting a string to a date) show you how to make the most of your favorite poorly decoupled external dependency!

function setPopupDate(targetID) {
   var dateInput = document.getElementById(targetID);
   var clockDate = new Date();
   var clockMonth = clockDate.getMonth() + 1;
   var clockDay = clockDate.getDate();
   var clockYear = clockDate.getFullYear();
   if (clockMonth < 10) {
      dateInput.innerHTML = "0" + clockMonth;
   } else {
      dateInput.innerHTML = clockMonth;
   }
   if (clockDay < 10) {
      dateInput.innerHTML = dateInput.innerHTML + "/0" + clockDay + "/" + clockYear;
   } else {
      dateInput.innerHTML = dateInput.innerHTML + "/" + clockDay + "/" + clockYear;
   }
}

No comments:

Post a Comment