如何以12小时AM/PM格式显示JavaScript日期时间?

如何以12小时AM/PM格式显示JavaScript日期时间?

如何以12小时格式(AM/PM)显示JavaScriptdatetime对象?



小怪兽爱吃肉
浏览 1663回答 3
3回答

江户川乱折腾

function formatAMPM(date) {&nbsp; var hours = date.getHours();&nbsp; var minutes = date.getMinutes();&nbsp; var ampm = hours >= 12 ? 'pm' : 'am';&nbsp; hours = hours % 12;&nbsp; hours = hours ? hours : 12; // the hour '0' should be '12'&nbsp; minutes = minutes < 10 ? '0'+minutes : minutes;&nbsp; var strTime = hours + ':' + minutes + ' ' + ampm;&nbsp; return strTime;}console.log(formatAMPM(new Date));

慕慕森

如果你只想显示时间那么.。var time = new Date();console.log(&nbsp; time.toLocaleString('en-US', { hour: 'numeric', hour12: true }));&nbsp;&nbsp;产出:上午7时如果你也想看会议记录的话.var time = new Date();console.log(&nbsp; time.toLocaleString('en-US', { hour: 'numeric', minute: 'numeric', hour12: true }));产出:上午7:23
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript