[英]Check if ISO String date is between two dates
我想检查中间日期是否在左右日期之间,如何在 Javascript 中完成此操作,它们是ISO String
。
From 2021-01-30T05:00:00.000Z <-> Date 2021-01-31T22:18:46Z <-> To 2021-01-31T05:00:00.000Z
如何验证2021-01-31T22:18:46Z
介于2021-01-30T05:00:00.000Z
和2021-01-31T05:00:00.000Z
function getElapsedMS(strOfD1, strOfD2) {
var d1 = new Date(strOfD1);
var d2 = new Date(strOfD2);
return d1-d2;
}
var d2d1 = getElapsedMS("2021-01-31T22:18:46Z", "2021-01-30T05:00:00.000Z");
var d2d3 = getElapsedMS("2021-01-31T22:18:46Z", "2021-01-31T05:00:00.000Z");
if (d2d1 * d2d3 < 0) { // can have more consideration to avoid overflow
//d2 is between d1 and d3
}
每个日期都可以表示为milliseconds since epoch
数,因此我们可以将每个日期转换为毫秒,并比较一个日期是否在另一个日期之后。
例子:
const dateA = new Date("2021-01-30T05:00:00.000Z");
const dateB = new Date("2021-01-31T05:00:00.000Z");
const dateC = new Date("2021-01-31T22:18:46Z");
// We use getTime() method to get the date as milliseconds since epoch
const isDateCBetweenAandB = dateA.getTime() <= dateC.getTime() && dateC.getTime() <= dateB.getTime();
console.log(isDateCBetweenAandB);
两个简单的选择...
const From = "2021-01-30T05:00:00.000Z" const date = "2021-01-31T22:18:46Z" const To = "2021-01-31T05:00:00.000Z" if (From.localeCompare(date) <= 0 && To.localeCompare(date) >= 0) { console.log("Date is in range") } else { console.log("Date is not in range") }
或将日期字符串解析为Date
实例或时间戳并进行数字比较
const From = new Date("2021-01-30T05:00:00.000Z") const date = new Date("2021-01-31T22:18:46Z") const To = new Date("2021-01-31T05:00:00.000Z") if (From <= date && To >= date) { console.log("Date is in range") } else { console.log("Date is not in range") }
注意:如果您使用的是 Typescript,它不喜欢在数字上比较Date
实例。 比较Date.prototype.valueOf()
或Date.prototype.getTime()
代替
你可以比较一下它的 unix 时间戳试试:
Date.parse('2021-01-31T22:18:46Z');
主要思想是我们只需要比较Date
。
但是,如果您使用.moment
或new Date()
来解析date-time UTC
,您会发现一个有趣的
moment("2021-01-31T22:18:46Z").format("YYYY-MM-DD") // "2021-02-01"
new Date("2021-01-31T22:18:46Z") // Mon Feb 01 2021 05:18:46 GMT+0700 (Indochina Time)
因此,如果compareDate
像2021-01-31T05:00:00.000Z
,那么您将得到预期的结果。
const fromDate = moment("2021-01-30T05:00:00.000Z").format("YYYY-MM-DD"); const conpareDate = moment("2021-01-31T05:00:00.000Z").format("YYYY-MM-DD"); const toDate = moment("2021-01-31T05:00:00.000Z").format("YYYY-MM-DD"); console.log({fromDate, conpareDate, toDate}); console.log(moment(conpareDate).isBetween(fromDate, toDate, undefined, '(]'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
如果还想准确比较日期,可以通过这种方式实现(但说实话,不推荐)。
const fromDate = '2021-01-30T05:00:00.000Z'.split('-'); const conpareDate = '2021-01-31T22:18:46Z'.split('-'); const toDate = '2021-01-31T05:00:00.000Z'.split('-'); const partStringToInt = (str) => parseInt(str, 10); const compareBetween = (a, b, c) => partStringToInt(a) <= partStringToInt(b) && partStringToInt(b) <= partStringToInt(c); const result = compareBetween(fromDate[0], conpareDate[0], toDate[0]) // Year && compareBetween(fromDate[1], conpareDate[1], toDate[1]) // Month && compareBetween(fromDate[2].substring(0, 2), conpareDate[2].substring(0, 2), toDate[2].substring(0, 2)); // Day console.log(result);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.