Javascript将日期更改为格式(dd / mm / yyyy)
问题描述:
如何转换以下日期格式(2012年11月19日13:29:40)
How can I convert the following date format below (Mon Nov 19 13:29:40 2012)
进入:
dd / mm / yyyy
dd/mm/yyyy
<html>
<head>
<script type="text/javascript">
function test(){
var d = Date()
alert(d)
}
</script>
</head>
<body>
<input onclick="test()" type="button" value="test" name="test">
</body>
</html>
答
有些JavaScript引擎可以直接解析这种格式,这使得任务很简单:
Some JavaScript engines can parse that format directly, which makes the task pretty easy:
function convertDate(inputFormat) {
function pad(s) { return (s < 10) ? '0' + s : s; }
var d = new Date(inputFormat);
return [pad(d.getDate()), pad(d.getMonth()+1), d.getFullYear()].join('/');
}