JS Date对象的使用

作者: puxiaotaoc | 来源:发表于2018-09-11 18:55 被阅读1次
// 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

相关文章

  • 15 js10 date对象

    date日期对象:js内置对象,使用构造函数创建; 1、var date = new Date();//创建对象;...

  • JS标准库系列(五)—— Date对象

    一、Date作为工具函数 Date对象是JS提供的日期和时间的操作接口。Date对象作为工具函数直接使用,不管有没...

  • JS Date对象的使用

  • Math数组及date

    Date对象: Date对象是js提供的日期和时间接口;Date对象有几个静态方法(即直接通过date对象调用的方...

  • JS总结概要

    1.JS事件 2.js内置对象 2.1Date 日期对象 var d = new Date(2012, 10, 1...

  • 2016.12.1 JS

    日期时间对象 var date=new Date() new:关键字 作用:创建对象 Date是个js内置的构造...

  • JS Date对象

  • JS Date对象

    Date对象是 JavaScript 原生的时间库。它以国际标准时间(UTC)1970年1月1日00:00:00作...

  • JS Date对象

    创建 Date 对象: new Date();以下四种方法同样可以创建 Date 对象: Date 对象属性 Da...

  • js Date 对象

    new Date()返回当日的日期和时间。 获取时间 getDate()从 Date 对象返回一个月中的某一天 (...

网友评论

    本文标题:JS Date对象的使用

    本文链接:https://www.haomeiwen.com/subject/fyougftx.html