ant design中时间控件的日期格式化

18 篇文章 0 订阅

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;
  }

第二种方式:

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

2、将时间转化为时间戳

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

3、时间格式化

第一种:获取带‘-’的日期

 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;
  }

5、获取下一个工作日

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值