使用 JavaScript 的 With 语句进行日期时间格式化

以下示例展示了如何使用 JavaScript 的 with 语句来格式化当前日期和时间:

var current_time = new Date();
with(current_time) {
  var strDate = getYear() + '年';
  strDate += getMonth() + '月';
  strDate += getDate() + '日';
  strDate += getHours() + ':';
  strDate += getMinutes() + ':';
  strDate += getSeconds();
  alert(strDate);
}

此代码通过 with 语句简化了对 Date 对象方法的调用,并将当前时间格式化为“年 月 日 时:分:秒”的形式。

ppt 文件大小:995KB