javascript - TypeScript 中的数据范围
问题描述
更新:
我有几个具有开始日期和结束日期的对象
插入/修改对象时,新对象的开始或结束日期不能包含在现有对象中。
Exist : 06/06/2018-----30/06/2018
can input:
04/06/2018-------05/06/2018
02/02/2018------ until less than date Start of which exists (05/06/2018)
or higher:
31/06/2018--------
can not get in :
04/06/2018-------07/06/2018 The end is already understood between the beginning and the end exists.
Or
07/06/2018---08/06/2018 because it is also included between the beginning and the end of the.
代码:
validateParamsDates(url, newConfig) {
return this.http.get<any>(url).pipe(map(param => {
let messageError = { message: "", showAlert: false };
let userStart = newConfig.startdatevalidity;
let userFinish = newConfig.enddatevalidity;
param[1]['params'].array.forEach(element => {
if(userStart > element.startdatevalidity && userFinish > element.enddatevalidity
|| userStart < element.startdatevalidity && userFinish < element.enddatevalidity
&& userStart > element.enddatevalidity
){
console.log('good');
}else{
console.log('=(');
}
});
return messageError ;
}));
}
解决方案
您应该首先将字符串转换为Date
对象。然后你可以比较日期,一切都会正常运行;)
所以你会做这样的事情:
const start2: Date = new Date(Object2.start);
const end1: Date = new Date(Object1.end);
if (start2 > end1) { console.log('good'); }
另请注意,为了让所有这些都在 javascript 中工作,日期应该以MM/DD/YYYY
格式定义,而不是DD/MM/YYYY
推荐阅读
- php - 我可以在自定义古腾堡块内的选项卡上将 inner_blocks 设置为 true 吗?
- asp.net-core - 访问我的 asp.net 核心控制台应用程序中的 appsettings.json 自定义部分
- python - Django - 根据引用模型中的现有值创建 ForeignKey 引用
- webhooks - 使用 Zapier webhooks 到 google 表格的 Webflow
- node.js - 如何修复nodejs中的意外令牌错误firebase部署
- excel - “UseRWHlinkNavigation”阻止 excel 超链接在浏览器中打开
- python - 多维数组的 Numba 实现非常慢
- delphi - 如何停靠存储在 DLL 或其他应用程序中的表单
- node.js - 在回调中使用 multer
- node.js - nodejs中的异步等待mongodb查询不起作用