方法类

1、

// 日期格式化
function zeroFill(s) {
  return s < 10 ? '0' + s : s;
}

// 获取当前时间  如果需要年月日可以不返回
export function nowTime(dateTime: any = Date.now()) {
  const date = new Date(dateTime)
  let time = date.getFullYear() + '-' + zeroFill((date.getMonth() + 1)) + '-' + zeroFill(date.getDate()) + ' ' + zeroFill(date.getHours()) + ':' + zeroFill(date.getMinutes()) + ':' + zeroFill(date.getSeconds())
  return time
}

// 获取当月第一天
export function dateStart() {
  const date = new Date()
  date.setDate(1)
  let dateStart = date.getFullYear() + '-' + zeroFill((date.getMonth() + 1)) + '-' + zeroFill(date.getDate())
  return dateStart
}
// 获取当前天
export function dateToday(date?) {
  date = date ? new Date(date) : new Date();
  let dateStart = date.getFullYear() + '-' + zeroFill((date.getMonth() + 1)) + '-' + zeroFill(date.getDate())
  return dateStart
}

// 获取N天前后
export function getDay(day) {
  var time = new Date();
  time.setDate(time.getDate() + day);//获取Day天后的日期 
  var y = time.getFullYear();
  var m = time.getMonth() + 1;//获取当前月份的日期 
  var d = time.getDate();
  return y + "-" + zeroFill(m) + "-" + zeroFill(d);
}
// 获取某个时间前后N天
export function getDayCurrent(curTime, day) {
  var time = new Date(curTime);
  time.setDate(time.getDate() + day);//获取Day天后的日期 
  var y = time.getFullYear();
  var m = time.getMonth() + 1;//获取当前月份的日期 
  var d = time.getDate();
  return y + "-" + zeroFill(m) + "-" + zeroFill(d);
}

// 获取N月前后
export function month(month, curruentTime?) {
  let time = curruentTime ? curruentTime : new Date();
  time.setDate(time.getDate());//获取Day天后的日期 
  let y = time.getFullYear();
  let m;

  if (time.getMonth() + month + 1 > 12) {
    y = y + 1;
    m = time.getMonth() + month - 11;
  } else if (time.getMonth() + month + 1 <= 0) {
    y = y - 1;
    m = time.getMonth() + month + 13;
  } else {
    m = time.getMonth() + month + 1;//获取当前月份的日期 d
  }
  let d = time.getDate();
  return y + "-" + zeroFill(m) + "-" + zeroFill(d);
}
// 获取某个时间N月前后
export function getMonthCurrent(curTime, month) {
  let time = new Date(curTime);
  time.setDate(time.getDate());//获取Day天后的日期 
  let y = time.getFullYear();
  let m;

  if (time.getMonth() + month + 1 > 12) {
    y = y + 1;
    m = time.getMonth() + month - 11;
  } else if (time.getMonth() + month + 1 <= 0) {
    y = y - 1;
    m = time.getMonth() + month + 13;
  } else {
    m = time.getMonth() + month + 1;//获取当前月份的日期 d
  }
  let d = time.getDate();
  return y + "-" + zeroFill(m) + "-" + zeroFill(d);
}
export function toDecimal2(val) {
  var num = parseFloat(val);
  if (isNaN(num)) return false;
  var num = Math.round(val * 100) / 100;
  return num;
}

你可能感兴趣的:(方法类)