Skip to main content

Ivan Teoh

Something personal yet public

167-exercise48.js

(Source)

function formatDate(date) {
    var month = date.getMonth() + 1;
    var day = date.getDate();
  return ((day < 10)? ("0" + day) : day) +
         "/" + ((month < 10)? ("0" + month) : month) +
         "/" + date.getFullYear();
}

document.write(formatDate(new Date(2010, 11, 25))); // "25/12/2010"
document.write(formatDate(new Date(2011, 1, 4))); // "04/02/2011"