angular - 以角度向当前 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);
}
解决方案
不太了解您需要什么,但如果您想在 Date 类型中添加天数,您只需这样做:
const today = new Date();
const fiveNextDays = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 5);
推荐阅读
- javascript - 过滤 JSON 数组中的数据
- jquery - Angular 6 ngOnInit 第一次不工作
- laravel - Laravel 一页中的所有最新活动
- php - laravel Form::open 和 Form::model 有什么区别
- node.js - 使用 node-imap,我如何将电子邮件移动到另一个文件夹?
- angular - 在 azure 中部署时未在 cookie 中设置防伪令牌
- angular - ngx-datatable cellClass 作为 templateRef 中的函数
- php - 突出显示搜索词
- c# - 读取 MassTransit Response-Request 中的响应标头
- elasticsearch - Elasticsearch 限制映射,以便只插入单个实体而不是数组