var now = new Date();
var year = now.getFullYear(); //年
var month = now.getMonth() + 1; //月
var day = now.getDate(); //日
var hh = now.getHours(); //时
var mm = now.getMinutes(); //分
var clock = year + "-";
if (month < 10)
clock += "0";
clock += month + "-";
if (day < 10)
clock += "0";
clock += day + " ";
if (hh < 10)
clock += "0";
clock += hh + ":";
if (mm < 10) clock += '0';
clock += mm;
alert(clock);

本文介绍了一个使用JavaScript进行日期和时间格式化的具体实现方法。通过获取当前日期的不同部分(如年、月、日、小时和分钟),然后根据这些部分构建一个符合特定格式的字符串。这个过程包括了对不同长度的月份和日期进行前导零填充。
7万+

被折叠的 条评论
为什么被折叠?



