Javascript Data Type How to - Convert a date 2011-08-13T10:38:27 in dd /mm/yy format








Question

We would like to know how to convert a date 2011-08-13T10:38:27 in dd /mm/yy format.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
<!--   w  w w .ja  v a2 s . co  m-->
function formatDate(dateparam) {
    var dateObj = new Date(Date.parse(dateparam));
    var date = dateObj.getDate();
    date = (date.toString().length == 1) ? "0" + date : date;
    var month = dateObj.getMonth();
    month++;
    month = (month.toString().length == 1) ? "0" + month : month;
    var year = dateObj.getFullYear();
    return month + "/" + date + "/" + year;
}
document.write(formatDate("2011-08-13 05:38:27"));

</script>
</head>
<body>
</body>
</html>

The code above is rendered as follows: