js datetime相关函数 本月本季度本年 下月 下季度 下年第一天

export default {
  formatNumber: n => {
    n = n.toString()
    return n[1] ? n : '0' + n
  },
  formatTime3: date => {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()
    return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  },
  formatTime2: date => {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()
    return [year, month, day].map(formatNumber).join('/')
  },

  //格式化时间
  formatTime: function (time, option) {
    time = +time * 1000;
    const d = new Date(time);
    const now = Date.now();
    const diff = (now - d) / 1000;
    if (diff < 30) {
      return '刚刚'
    } else if (diff < 3600) { // less 1 hour
      return Math.ceil(diff / 60) + '分钟前'
    } else if (diff < 3600 * 24) {
      return Math.ceil(diff / 3600) + '小时前'
    } else if (diff < 3600 * 24 * 2) {
      return '1天前'
    }
    if (option) {
      return parseTime(time, option)
    } else {
      return d.getMonth() + 1 + '月' + d.getDate() + '日' + d.getHours() + '时' + d.getMinutes() + '分'
    }
  },
  //-------------------------------------
  // format
  //-------------------------------------
  format: function (time) {
    if (time != null) {
      var date = new Date(parseInt(time));
      return this.formatDate(date, 'yyyy-MM-dd hh:mm') //'yyyy-MM-dd hh:mm'
    }
  },
  format_date: function (date) {
    if (date != null) {
      return this.formatDate(date, 'yyyy-MM-dd') //'yyyy-MM-dd hh:mm'
    }
  },
  format_date_cn: function (date) {
    if (date != null) {
      return this.formatDate(date, 'yyyy年MM月dd日')
    }
  },
  format2: function (time) {
    if (time != null) {
      var tt = parseInt(time)
      if (tt < 1000000000000) {
        tt = tt * 1000
      }
      var date = new Date(tt);
      return this.formatDate(date, 'yyyy-MM-dd hh:mm') //'yyyy-MM-dd hh:mm'
    }
  },


  dateFormat: function (timestamp, format) {
    if (timestamp == null || timestamp.length < 13) {
      return ''
    }
    if (!format) {
      format = "yyyy-MM-dd hh:mm:ss";
    }
    if (typeof (timestamp) == 'string') {
      timestamp = parseInt(timestamp);
    }
    var date = new Date(timestamp);
    return this.formatDate(date, format)
  },



  formatDate: function (date, fmt) {
    if (/(y+)/.test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    let o = {
      'M+': date.getMonth() + 1,
      'd+': date.getDate(),
      'h+': date.getHours(),
      'm+': date.getMinutes(),
      's+': date.getSeconds()
    };
    for (let k in o) {
      if (new RegExp(`(${k})`).test(fmt)) {
        let str = o[k] + '';
        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : this.padLeftZero(str));
      }
    }
    return fmt;
  },

  padLeftZero: function (str) {
    return ('00' + str).substr(str.length);
  },
  //格式化时间
  parseTime: function (time, cFormat) {
    if (arguments.length === 0) {
      return null;
    }
    const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}';
    let date;
    if (typeof time == 'object') {
      date = time;
    } else {
      if (('' + time).length === 10) time = parseInt(time) * 1000;
      date = new Date(time);
    }
    const formatObj = {
      y: date.getFullYear(),
      m: date.getMonth() + 1,
      d: date.getDate(),
      h: date.getHours(),
      i: date.getMinutes(),
      s: date.getSeconds(),
      a: date.getDay()
    };
    const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
      let value = formatObj[key];
      if (key === 'a') return ['一', '二', '三', '四', '五', '六', '日'][value - 1];
      if (result.length > 0 && value < 10) {
        value = '0' + value;
      }
      return value || 0;
    });
    return time_str;
  },


  //90天前的时间
  getTime: function (type) {
    if (type === 'start') {
      return new Date().getTime() - 3600 * 1000 * 24 * 90
    } else {
      return new Date(new Date().toDateString())
    }
  },

  /**
   * 获取指定日期的周的第一天、月的第一天、季的第一天、年的第一天
   * @param date new Date()形式,或是自定义参数的new Date()
   * @returns 返回值为格式化的日期,yy-mm-dd
   */

  //获取这周的周一
  getFirstDayOfWeek: function (date) {
    var weekday = date.getDay() || 7; //获取星期几,getDay()返回值是 0(周日) 到 6(周六) 之间的一个整数。0||7为7,即weekday的值为1-7
    date.setDate(date.getDate() - weekday + 1); //往前算(weekday-1)天,年份、月份会自动变化
    return date //timeFormat(date);
  },

  //获取当月第一天
  getFirstDayOfMonth: function (date) {
    date.setDate(1);
    return date //timeFormat(date);
  },
  //获取下月第一天
  getFirstDayOfNextMonth: function (date) {
    date.setDate(1);
    let m = date.getMonth();
    if (m == 11) {
      //获取下一年的第一天
      return this.getFirstDayOfNextYear(date)
    } else {
      date.setMonth(date.getMonth() + 1)
      return date //timeFormat(date);
    }
  },
  //获取当季第一天
  getFirstDayOfSeason: function (date) {
    var month = date.getMonth();
    if (month < 3) {
      date.setMonth(0);
    } else if (2 < month && month < 6) {
      date.setMonth(3);
    } else if (5 < month && month < 9) {
      date.setMonth(6);
    } else if (8 < month && month < 11) {
      date.setMonth(9);
    }
    date.setDate(1);
    return date //timeFormat(date);
  },

  //获取下季度第一天
  getFirstDayOfNextSeason: function (date) {
    var month = date.getMonth();
    if (month < 3) {
      date.setMonth(3);
    } else if (2 < month && month < 6) {
      date.setMonth(6);
    } else if (5 < month && month < 9) {
      date.setMonth(9);
    } else if (8 < month && month < 11) {
      return this.getFirstDayOfNextYear(date) //下年的第一天
    }
    date.setDate(1);
    return date //timeFormat(date);
  },
  //获取当年第一天
  getFirstDayOfYear: function (date) {
    date.setDate(1);
    date.setMonth(0);
    return date //timeFormat(date);
  },
  //获取下年第一天
  getFirstDayOfNextYear: function (date) {
    date.setDate(1);
    date.setMonth(0); //获取当年第一天
    date.setFullYear(date.getFullYear() + 1);
    // date.setDate(date.getDate() - 1); 
    return date;
  },

  // 1.获取今天的0时0分0秒(常用于开始日期的获取)
  getToday00: function () {
    var startDate = new Date(new Date().toLocaleDateString()); //Tue May 15 2018 00:00:00 GMT+0800 (中国标准时间)
    return startDate;
  },
  // 2.获取一个月前的日期 
  getPerMonthDate: function () {
    var lastM = new Date(new Date().setMonth(new Date().getMonth() - 1));//Sun Apr 15 2018 09:18:08 GMT+0800 (中国标准时间)
    return lastM
  },
  // 3.获取一个月前的0时0分0秒
  getPerMonthDate00: function () {
    var lastM_start = new Date(new Date(new Date().toLocaleDateString()).setMonth(new Date().getMonth() - 1));
    //Sun Apr 15 2018 00:00:00 GMT+0800 (中国标准时间)
    return lastM_start
  },
  // 4.获取前一天的日期
  getPerDay: function () {
    var yesterday = new Date(new Date().setDate(new Date().getDate() - 1));//Mon May 14 2018 09:26:39 GMT+0800 (中国标准时间)
    return yesterday
  },
  // 5.获取今天的23时59分59秒
  getTodayEndTime: function () {
    var endDate = new Date(new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1);
    //Tue May 15 2018 23:59:59 GMT+0800 (中国标准时间) 
    return endDate;
  },
  // 6.获取昨天的23时59分59秒
  getYestdayEndTime: function () {
    var yes_endDate = new Date(new Date(new Date(
      new Date().setDate(new Date().getDate() - 1)).toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1);
    //Mon May 14 2018 23:59:59 GMT+0800 (中国标准时间) 
    return yes_endDate
  },
  /**
   * 获取时差的字符串
   * time_diff 相差的时间戳
   */
  get_diff: function (time_diff) {
    // console.log('time_diff = ' + time_diff)
    var days = Math.floor(time_diff / (24 * 3600 * 1000)); //计算出相差天数
    var leave1 = time_diff % (24 * 3600 * 1000) //计算天数后剩余的毫秒数
    var hours = Math.floor(leave1 / (3600 * 1000)) //计算出小时数
    //计算相差分钟数
    var leave2 = leave1 % (3600 * 1000) //计算小时数后剩余的毫秒数
    var minutes = Math.floor(leave2 / (60 * 1000)) //计算相差分钟数
    //计算相差秒数
    var leave3 = leave2 % (60 * 1000) //计算分钟数后剩余的毫秒数
    var seconds = Math.round(leave3 / 1000)
    // console.log(" 相差 " + days + "天 " + hours + "小时 " + minutes + " 分钟" + seconds + " 秒")

    var diff = ''
    if (days > 0) {
      diff += days + '天 '
    }
    diff += hours + ':' + minutes + ':' + seconds

    if (hours < 0 || minutes < 0 || seconds < 0) {
      diff = '00:00:00'
    }
    return diff
  },



}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值