js 获取日期相关问题

1.获取三个月之前的日期

export function get3MonthBefor() {
  var resultDate, year, month, date, hms;
  var currDate = new Date();
  year = currDate.getFullYear();
  month = currDate.getMonth() + 1;
  date = currDate.getDate();
  hms = currDate.getHours() + ':' + currDate.getMinutes() + ':' + (currDate.getSeconds() < 10 ? '0' + currDate.getSeconds() : currDate.getSeconds());
  switch (month) {
    case 1:
    case 2:
    case 3:
    month += 9;
    year--;
    break;
    default:
    month -= 3;
    break;
  }
  month = (month < 10) ? ('0' + month) : month;
  resultDate = year + '-' + month + '-' + date
  // resultDate = year + '-' + month + '-' + date + ' ' + hms;
  return resultDate;
}
 
2.时间戳转日期
 

export const timeFormat = (timestamp) => {
  if (timestamp) {
    var date = new Date(timestamp * 1000);
  } else {
  var date = new Date();
  }
  let Y = date.getFullYear(),
  m = date.getMonth() + 1,
  d = date.getDate(),
  H = date.getHours(),
  i = date.getMinutes(),
  s = date.getSeconds();
  if (m < 10) {
    m = '0' + m;
  }
  if (d < 10) {
    d = '0' + d;
  }
  if (H < 10) {
    H = '0' + H;
  }
  if (i < 10) {
    i = '0' + i;
  }
  if (s < 10) {
    s = '0' + s;
  }
  var t = Y + '-' + m + '-' + d + ' ' + H + ':' + i + ':' + s;
  return t;
}

原文地址:https://www.cnblogs.com/wusheng2016/p/8252261.html