我需要你的帮助,
我有前两种日期格式与服务器返回的长日期一起使用。但是,当我将函数 dateToMDY(date) 调用到第 3 个 div 时,日期格式仍为 dd/mm/yyyy。多么痛苦。我在这里做错了什么?一切看起来都井井有条。我错过了什么吗?
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
</style>
<script type="text/javascript">
function test() {
var x = "Mon Mar 5 00:00:00 EST 2012"
var y = "05/03/2012"
document.getElementById('divy1').innerHTML = dateToDMY(new Date(x))
document.getElementById('divy2').innerHTML = dateToMDY(new Date(x))
document.getElementById('divy3').innerHTML = dateToMDY(new Date(y))
}
function dateToDMY(date) {
var d = date.getDate();
var m = date.getMonth() + 1;
var y = date.getFullYear();
return ''+ (d <= 9 ? '0' + d : d) +'/'+ (m<=9 ? '0' + m : m) + '/' + y;
}
function dateToMDY(date) {
var d = date.getDate();
var m = date.getMonth() + 1;
var y = date.getFullYear();
return ''+ (m<=9 ? '0' + m : m)+'/'+ (d <= 9 ? '0' + d : d) + '/' + y;
}
</script>
</head>
<body onload="test()">
dd/mm/yyyy (DatePicker)
<div id="divy1"></div>
<br>
mm/dd/yyyy (SQL)
<div id="divy2"></div>
<br>
Default Date String should be converted to mm/dd/yyyy
<div id="divy3"></div>
</body>
</html>