首页 > 解决方案 > 以角度向当前 utc 日期添加天数

问题描述

我正在编写一个茉莉花测试,我需要在其中操作 UTC 日期。目前我能够以我想要的格式获得 UTC 日期。我在测试中提到我需要在格式化之前传递添加的日期。它的 component.GetUtcDate 调用

例如 2020-03-20T14:24:52.000

在将它传递给 GetUtcDate 方法之前,我需要添加日期。我怎么做 ?。最终说在添加 5 天后它应该显示 2020-03-25T14:24:52.000

let todayDate: Date;
let acceptanceDate: Date;
let acceptanceDateUtc: string;


fit('should call checkIfLastAgreement and notify that its the last agreement when  is called accept is called', () => {
    todayDate = new Date();
    acceptanceDate = component.GetUtcDate(NEED TO ADD DAYS TO THE DATE HERE);
    acceptanceDateUtc = acceptanceDate.toISOString();
    acceptanceDateUtc = acceptanceDateUtc.substring( 0,  acceptanceDateUtc.length - 1);
    component.currentIndex = 0;
    component.myData = [{userAgreementId: 1}, { acceptanceWindowExpiry: acceptanceDateUtc }];
    component.agreementLength = 2;
    component.lastAgreement = true;
    component.activeBtn = 0;
    component.accept();

    expect(component.agreementsService.updateAgreement).toHaveBeenCalled();
    expect(component.lastAgreement).toBe(true);
    expect(component.endOfAgreements).toBe(true);
  });

组件代码

 GetUtcDate(date: Date) {
    const now_utc =  Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(),
    date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds());

    return new Date(now_utc);
  }

标签: angular

解决方案


不太了解您需要什么,但如果您想在 Date 类型中添加天数,您只需这样做:

const today = new Date();

const fiveNextDays = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 5);

推荐阅读