Yes, you can use the native javascript Date() object and its methods.(是的,您可以使用本机javascript Date() 对象及其方法。)
For instance you can create a function like:(例如,您可以创建一个函数,如:)
function formatDate(date) {
var hours = date.getHours();
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 = hours + ':' + minutes + ' ' + ampm;
return date.getMonth()+1 + "/" + date.getDate() + "/" + date.getFullYear() + " " + strTime;
}
var d = new Date();
var e = formatDate(d);
alert(e);
And display also the am / pm and the correct time.(并显示上午/下午和正确的时间。)
Remember to use getFullYear() method and not getYear() because it has been deprecated.(请记住使用getFullYear()方法而不是getYear(),因为它已被弃用。)
DEMO http://jsfiddle.net/a_incarnati/kqo10jLb/4/(演示 http://jsfiddle.net/a_incarnati/kqo10jLb/4/) 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…