根据当前时间获得这周开始、结束日期及这月开始、结束日期

本文介绍在Angular项目中,如何在ngOnInit钩子中调用方法获取当前周和月的开始及结束日期。
摘要由CSDN通过智能技术生成

最近写angular2项目,时间显示时,有这个要求

在ngOnInit()钩子函数里调用方法

async ngOnInit() {
    const today = new Date();
    const str = '7123456'.charAt(new Date().getDay());
    // 这周开始时间 xxxx-xx-xx
   const date1 = this.getDay(- Number(str) + 1);
    // 这周结束时间
    const date2 = this.getDay(7 - Number(str));
    // 当月开始
    const startDate = this.CurrentMonthFirst(today);
    // 当月最后一天
    const endDate = this.CurrentMonthLast(today);
  }

实现函数

  // 显示最近一周的时间
  getDay(day) {
    const today = new Date();
    const time = today.getTime() + 1000 * 60 * 60 * 24 * day;
    // 注意,这行是关键代码
    today.setTime(time);
    const tYear = today.getFullYear();
    let tMonth = today.getMonth();
    let tDate = today.getDate();
    tMonth = this.doHandleMonth(tMonth + 1);
    tDate = this.doHandleMonth(tDate);
    return tYear +
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值