Timestamp convert time or get date utility class

Gets the first day of the current month

function getCurrentMonthFirst=()=>{
  var date=new Date();
  date.setDate(1);
  return common.getdateNoTime(date);
}
Copy the code

N days before obtaining the date

function getBeforeDate=()=>{
  var n = n;
  var d = new Date();
  var year = d.getFullYear();
  var mon = d.getMonth() + 1;
  var day = d.getDate();
  if (day <= n) {
    if (mon > 1) {
      mon = mon - 1;
    } else {
      year = year - 1;
      mon = 12;
    }
  }
  d.setDate(d.getDate() - n);
  year = d.getFullYear();
  mon = d.getMonth() + 1;
  day = d.getDate();
  const s = year + '-' + (mon < 10 ? '0' + mon : mon) + '-' + (day < 10 ? '0' + day : day);
  return s;
}
Copy the code

Judge the difference between the two dates

/** * @zhiparam sDate1 Start date for example: 2016-11-01 * @param sDate2 End date for example: 2016-11-02 * @returns {nDays} Returns the number of days */ function daysBetween = (sDate1, sDate2) => { var time1 = Date.parse(new Date(sDate1)); var time2 = Date.parse(new Date(sDate2)); var nDays = Math.abs(parseInt((time2 - time1) / 1000 / 3600 / 24)); return nDays; }Copy the code

According to bai, judge the month difference between two dates

/** * @zhiparam startDate startDate for example: 2016-11-01 * @param endStart end date for example: */ function getIntervalMonth = (startDate, endStart) => { var startMonth = new Date(startDate).getMonth(); var endMonth = new Date(endStart).getMonth(); var intervalMonth = new Date(endStart).getFullYear() * 12 + endMonth - (new Date(startDate).getFullYear() * 12 + startMonth); return intervalMonth; }Copy the code

Gets the date entered several months ago

/** *{param:DateTime} date Date (YYYY-MM-DD) *{param:number} monthNum */ function getIntervalMonth = (startDate, monthDate) endStart) => { var dateArr = date.split('-'); var year = dateArr[0]; Var month = dateArr[1]; Var day = dateArr[2]; Var days = new Date(year, month, 0); days = days.getDate(); Var year2 = year; var month2 = parseInt(month) - monthNum; if (month2 <= 0) { var absM = Math.abs(month2); year2 = parseInt(year2) - Math.ceil(absM / 12 == 0 ? 1 : parseInt(absM) / 12); month2 = 12 - (absM % 12); } var day2 = day; var days2 = new Date(year2, month2, 0); days2 = days2.getDate(); if (day2 > days2) { day2 = days2; } if (month2 < 10) { month2 = '0' + month2; } var t2 = year2 + '-' + month2 + '-' + day2; return t2; }Copy the code

Timestamp conversion time

function getdate= (date) => {
  var now = new Date(date),
    y = now.getFullYear(),
    m = now.getMonth() + 1,
    d = now.getDate();
  return y + '-' + (m < 10 ? '0' + m : m) + '-' + (d < 10 ? '0' + d : d) + ' ' + now.toTimeString().substr(0, 8);
}
Copy the code

Timestamp conversion time – every minute and second

function getdateNoTime= (date) => {
  var now = new Date(date),
    y = now.getFullYear(),
    m = now.getMonth() + 1,
    d = now.getDate();
  return y + '-' + (m < 10 ? '0' + m : m) + '-' + (d < 10 ? '0' + d : d);
}
Copy the code

Timestamp conversion time – no date

function getdateTime= (date) => {
  var now = new Date(date),
    y = now.getFullYear(),
    m = now.getMonth() + 1,
    d = now.getDate();
  return now.toTimeString().substr(0, 8);
}
Copy the code

Get the current date

function formatting= (time) => { let date = new Date(); if (time ! == undefined) { date = new Date(time); } const seperator1 = '-'; const year = date.getFullYear(); let month = date.getMonth() + 1; let strDate = date.getDate(); if (month >= 1 && month <= 9) { month = `0${month}`; } if (strDate >= 0 && strDate <= 9) { strDate = `0${strDate}`; } const currentdate = year + seperator1 + month + seperator1 + strDate; return currentdate; }Copy the code