ant design的日期时间控件的日期时间格式化方式-moment

将时间戳转化为时间

转换方式1-自定义方法

timestampToTime(str) {
    const date = new Date(str);
    const Y = `${date.getUTCFullYear()}-`;
    const M = `${date.getUTCMonth() + 1 < 10 ? `0${date.getUTCMonth() + 1}` : date.getUTCMonth() + 1}-`;
    const D = `${date.getUTCDate()} `;
    const h = `${date.getUTCHours() < 10 ? `0${date.getUTCHours()}` : date.getUTCHours()}:`;
    const m = `${date.getUTCMinutes() < 10 ? `0${date.getUTCMinutes()}` : date.getUTCMinutes()}:`;
    const s = date.getUTCSeconds() < 10 ? `0${date.getUTCSeconds()}` : date.getUTCSeconds();
    return Y + M + D + h + m + s;
  }

转换方式2-使用moment

npm i moment

dateToString(timestamp) {
    return moment(timestamp).format('YYYY-MM-DD HH:mm:ss');
}

将时间转化为时间戳

 TimesTamp(str) {
    // 获取某个时间格式的时间戳
    const stringTime = str;
    let timestamp = Date.parse(new Date(stringTime));
    timestamp /= 1000;
    return timestamp;
}

时间格式化

获取带 - 的日期

 formatDate(oDate, str) {
    const yy = oDate.getFullYear();
    let mm = oDate.getMonth() + 1;
    let dd = oDate.getDate();
    mm = mm < 10 ? `0${mm}` : mm;
    dd = dd < 10 ? `0${dd}` : dd;
    const dateStr = `${yy + str + mm + str + dd}`;
    return dateStr;
}

获取日期:前天、昨天、今天、明天、后天

GetDateStr(AddDayCount) {
    const dd = new Date();
    dd.setDate(dd.getDate() + AddDayCount); // 获取AddDayCount天后的日期
    const y = dd.getFullYear();
    const m = dd.getMonth() + 1; // 获取当前月份的日期
    const d = dd.getDate();
    const today = `${y}-${m}-${d}`;
    const defaultQuery = {
      endTime: today,
    };
    return defaultQuery;
}

获取默认今天

// 获取默认今天
  getBeforeTodayDate() {
    const today = new Date()
      .toLocaleDateString()
      .split('/')
      .join('-');
    const defaultQuery = {
      endTime: today,
    };
    return defaultQuery;
  },
  getToday() {
    const todayDate = new Date();
    const year = todayDate.getFullYear();
    const month = todayDate.getMonth() + 1 > 9 ? `${todayDate.getMonth() + 1}` : `0${todayDate.getMonth() + 1}`;
    const day = todayDate.getDate() > 9 ? `${todayDate.getDate()}` : `0${todayDate.getDate()}`;
    const today = year + month + day;
    return today;
  },
  getTodayFormat() {
    const todayDate = new Date();
    const year = todayDate.getFullYear();
    const month = todayDate.getMonth() + 1 > 9 ? `${todayDate.getMonth() + 1}` : `0${todayDate.getMonth() + 1}`;
    const day = todayDate.getDate() > 9 ? `${todayDate.getDate()}` : `0${todayDate.getDate()}`;
    const today = `${year}-${month}-${day}`;
    return today;
  },

获取上一个工作日

getLastWorkDay() {
    const day = moment().day();
    const days = day === 1 ? 3 : day === 0 ? 2 : 1; // 周一取值为上周五,周日取值为本周五,其他日期取值为前一天
    const lastWorkDay = moment()
      .add(-days, 'd')
      .format('YYYY-MM-DD');
    return lastWorkDay;
}

获取下一个工作日

function getNextWorkDay(dateFormat) {
  const day = moment().day();
  const days = day !== 5 ? 1 : day === 6 ? 2 : 3;
  return moment()
    .add(+days, 'd')
    .format(dateFormat || 'YYYYMMDD');
}

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 鲸 设计师:meimeiellie 返回首页