前端面试基础必备JS学习笔记

JS Date对象的使用

2018-09-11  本文已影响1人  puxiaotaoc
// Date 对象用于处理日期和时间
  var date = new Date();
  console.log(date); // Tue Sep 11 2018 18:19:44 GMT+0800 (中国标准时间)
  console.log(date.getTime()); // 1536661466696 返回 1970 年 1 月 1 日至今的毫秒数
  console.log(date.getDate()); // 11 从 Date 对象返回一个月中的某一天 (1 ~ 31)
  console.log(date.getDay()); // 2  从 Date 对象返回一周中的某一天 (0 ~ 6)
  console.log(date.getMonth()); // 8 从 Date 对象返回月份 (0 ~ 11)
  console.log(date.getFullYear()); // 2018 从 Date 对象以四位数字返回年份
  console.log(date.getHours()); // 18 返回 Date 对象的小时 (0 ~ 23)
  console.log(date.getMinutes()); // 31 返回 Date 对象的分钟 (0 ~ 59)
  console.log(date.getSeconds()); // 58 返回 Date 对象的秒数 (0 ~ 59)
  console.log(date.getMilliseconds()); // 777 返回 Date 对象的毫秒(0 ~ 999)
  console.log(Date.parse('Jul 8, 2005')); // 1120752000000 返回1970年1月1日午夜到指定日期(字符串)的毫秒数
  date.setDate(2);
  date.setMonth(2);
  date.setFullYear(2016);
  date.setHours(2);
  date.setMinutes(2);
  date.setSeconds(2);
  date.setMilliseconds(2);
  console.log(date); // Wed Mar 02 2016 02:02:02 GMT+0800 (中国标准时间)
  date.setTime(77771564221); // 向 1970/01/01 添加 77771564221 毫秒,并显示新的日期和时间
  console.log(date); // Mon Jun 19 1972 11:12:44 GMT+0800 (中国标准时间)
  console.log(date.toString()); // Mon Jun 19 1972 11:12:44 GMT+0800 (中国标准时间) 把 Date 对象转换为字符串
  console.log(date.toTimeString()); // 11:12:44 GMT+0800 (中国标准时间) 把 Date 对象的时间部分转换为字符串
  console.log(date.toDateString()); // Mon Jun 19 1972 把 Date 对象的日期部分转换为字符串
  console.log(date.toLocaleString()); // 1972/6/19 上午11:12:44 根据本地时间格式,把 Date 对象转换为字符串
  console.log(date.toLocaleTimeString()); // 上午11:12:44 根据本地时间格式,把 Date 对象的时间部分转换为字符串
  console.log(date.toLocaleDateString()); // 1972/6/19 根据本地时间格式,把 Date 对象的日期部分转换为字符串
  console.log(date.valueOf()); // 77771564221 返回 Date 对象的原始值
  // 日期转时间戳
  // 方法一 获取当前时间戳,会精确到毫秒
  var date = new Date();
  console.log(date.getTime()); // 1536675550877
  // 方法二 获取当前时间戳,只能精确到秒,毫秒将用0来代替
  var date = new Date();
  console.log(Date.parse(date)); // 1536675661000 
  // 方法三 获取当前时间戳,会精确到毫秒
  var date = new Date();
  console.log(date.valueOf()); // 1536675764181
// 获取某个格式的时间戳
  var timestamp = '2018/9/11/00:00:00';
  var _timestamp = new Date(timestamp).getTime();
  var _timestamp = _timestamp / 1000;
  console.log(`${timestamp} 的时间戳为:${_timestamp}`); // 2018/9/11/00:00:00 的时间戳为:1536595200
  var date = new Date();
  console.log(date); // Thu Sep 13 2018 21:51:58 GMT+0800 (中国标准时间)
  console.log(date.toLocaleString()); // 2018/9/13 下午9:51:58
  var date = new Date(); 
  console.log(date); // 'Thu Sep 13 2018 21:51:58 GMT+0800 (中国标准时间)' 
  // 返回 Date 对象的原始值,以毫秒表示
  console.log(date.valueOf()); // 1536846836528
  // 把 Date 对象转换为字符串,并返回结果。使用本地时间表示
  console.log(date.toString()); // 'Thu Sep 13 2018 21:53:56 GMT+0800 (中国标准时间)'
 // 可根据本地时间把 Date 对象转换为字符串,并返回结果,返回的字符串根据本地规则格式化
  console.log(date.toLocaleString()); // '2018/9/13 下午9:51:58'
// 日期格式化
  Date.prototype.format = function(format) {
    var date = {
      "M+": this.getMonth() + 1,
      "d+": this.getDate(),
      "h+": this.getHours(),
      "m+": this.getMinutes(),
      "s+": this.getSeconds(),
      "q+": Math.floor((this.getMonth() + 3) / 3),
      "S+": this.getMilliseconds()
    };
    if (/(y+)/i.test(format)) {
      format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    for (var k in date) {
      if (new RegExp("(" + k + ")").test(format)) {
        format = format.replace(RegExp.$1, RegExp.$1.length == 1 ?
          date[k] : ("00" + date[k]).substr(("" + date[k]).length));
      }
    }
    return format;
  }
  var newDate = new Date();
  console.log(newDate.format('yyyy-MM-dd h:m:s')); // 2018-09-12 9:6:39
上一篇 下一篇

猜你喜欢

热点阅读