我有以下测试脚本显示当前日期&时间 :-
document.getElementById("para1").innerHTML = formatAMPM(); function formatAMPM() { var date = new Date(); var hours = date.getHours(); var days = date.getDay(); var minutes = date.getMinutes(); var ampm = hours >= 12 ? 'pm' : 'am'; hours = hours % 12; hours = hours ? hours : 12; // the hour '0' should be '12' minutes = minutes < 10 ? '0'+minutes : minutes; var strTime = date + ' ' + hours + ':' + minutes + ' ' + ampm; return strTime; }
Fri Aug 30 2013 16:36:10 GMT+0100 (GMT Standard Time) 4:36 pm
Fri Aug 30 2013 4:36 pm
任何人可以建议我如何实现这一点?
解决方法
尝试这个:
var d = new Date(),minutes = d.getMinutes().toString().length == 1 ? '0'+d.getMinutes() : d.getMinutes(),hours = d.getHours().toString().length == 1 ? '0'+d.getHours() : d.getHours(),ampm = d.getHours() >= 12 ? 'pm' : 'am',months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],days = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat']; return days[d.getDay()]+' '+months[d.getMonth()]+' '+d.getDate()+' '+d.getFullYear()+' '+hours+':'+minutes+ampm;