Javascript Data Type How to - Format date as '1-Jan-01'








Question

We would like to know how to format date as '1-Jan-01'.

Answer


<!DOCTYPE html>
<html>
<head>
<!--   w  ww.j  ava 2  s .  c  o m-->
<script type='text/javascript'>

var monthAbbreviations = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
var dates = ['2001-01-01'];
var tmpDate = new Date(dates[0]);
var day = tmpDate.getUTCDate();
var mo = monthAbbreviations[tmpDate.getUTCMonth()];
var yr = String(tmpDate.getUTCFullYear()).substring(2,4);
var formattedDate = day + '-' + mo + '-' + yr;
document.writeln(formattedDate);

</script>
</head>
<body>

</body>
</html>

The code above is rendered as follows: