有人能提出一种方法来比较两个大于、小于和过去不使用JavaScript的日期的值吗?值将来自文本框。
当前回答
以上给出的所有答案只解决了一件事:比较两个日期。
事实上,它们似乎是这个问题的答案,但其中很大一部分是缺失的:
如果我想检查一个人是否满18岁怎么办?
不幸的是,上面给出的答案中没有一个能够回答这个问题。
例如,当前时间(大约是我开始键入这些单词的时间)是2020年1月31日星期五10:41:04 GMT-0600(中部标准时间),而客户将其出生日期输入为“01/31/2002”。
如果我们使用“365天/年”,即“31536000000”毫秒,我们将得到以下结果:
let currentTime = new Date();
let customerTime = new Date(2002, 1, 31);
let age = (currentTime.getTime() - customerTime.getTime()) / 31536000000
console.log("age: ", age);
打印如下:
age: 17.92724710838407
但从法律上讲,这位客户已经18岁了。即使他输入“01/30/2002”,结果仍然是
age: 17.930039743467784
其小于18。系统将报告“未成年”错误。
“01/29/2002”、“01/28/2002”和“01/27/2002”。。。“01/05/2002”,直到“01/04/2002”。
这样的系统只会杀死所有出生在18岁0天到18岁26天之间的客户,因为他们在法律上是18岁,而系统显示“未成年”。
以下是对类似问题的回答:
invalidBirthDate: 'Invalid date. YEAR cannot be before 1900.',
invalidAge: 'Invalid age. AGE cannot be less than 18.',
public static birthDateValidator(control: any): any {
const val = control.value;
if (val != null) {
const slashSplit = val.split('-');
if (slashSplit.length === 3) {
const customerYear = parseInt(slashSplit[0], 10);
const customerMonth = parseInt(slashSplit[1], 10);
const customerDate = parseInt(slashSplit[2], 10);
if (customerYear < 1900) {
return { invalidBirthDate: true };
} else {
const currentTime = new Date();
const currentYear = currentTime.getFullYear();
const currentMonth = currentTime.getMonth() + 1;
const currentDate = currentTime.getDate();
if (currentYear - customerYear < 18) {
return { invalidAge: true };
} else if (
currentYear - customerYear === 18 &&
currentMonth - customerMonth < 0) {
return { invalidAge: true };
} else if (
currentYear - customerYear === 18 &&
currentMonth - customerMonth === 0 &&
currentDate - customerDate < 0) {
return { invalidAge: true };
} else {
return null;
}
}
}
}
}
其他回答
使用momentjs进行日期操作。
使用
isSameOrAfter()方法
moment('2010-10-20').isSameOrAfter('2010-10-20') //true;
使用isAfter()方法检查一个日期是否在另一个日期之后
moment('2020-01-20').isAfter('2020-01-21'); // false
moment('2020-01-20').isAfter('2020-01-19'); // true
使用isBefore()方法检查一个日期是否早于另一个日期。
moment('2020-01-20').isBefore('2020-01-21'); // true
moment('2020-01-20').isBefore('2020-01-19'); // false
使用isSame()方法检查一个日期与另一个日期是否相同
moment('2020-01-20').isSame('2020-01-21'); // false
moment('2020-01-20').isSame('2020-01-20'); // true
您可以使用以下代码:
var firstValue = "2012-05-12".split('-');
var secondValue = "2014-07-12".split('-');
var firstDate=new Date();
firstDate.setFullYear(firstValue[0],(firstValue[1] - 1 ),firstValue[2]);
var secondDate=new Date();
secondDate.setFullYear(secondValue[0],(secondValue[1] - 1 ),secondValue[2]);
if (firstDate > secondDate)
{
alert("First Date is greater than Second Date");
}
else
{
alert("Second Date is greater than First Date");
}
还要检查Date类的MDN文章。
If you are using **REACT OR REACT NATIVE**, use this and it will work (Working like charm)
如果两个日期相同,则返回TRUE,否则返回FALSE
const compareDate = (dateVal1, dateVal2) => {
if (dateVal1.valueOf() === dateVal2.valueOf()){
return true;
}
else { return false;}
}
尝试使用此代码
var f =date1.split("/");
var t =date2.split("/");
var x =parseInt(f[2]+f[1]+f[0]);
var y =parseInt(t[2]+t[1]+t[0]);
if(x > y){
alert("date1 is after date2");
}
else if(x < y){
alert("date1 is before date2");
}
else{
alert("both date are same");
}
Date对象将执行您想要的操作-为每个日期构造一个,然后使用>、<、<=或>=对它们进行比较。
==,!=,==,和!==运算符要求您使用date.getTime(),如中所示
var d1 = new Date();
var d2 = new Date(d1);
var same = d1.getTime() === d2.getTime();
var notSame = d1.getTime() !== d2.getTime();
明确地说,直接检查日期对象是否相等是行不通的
var d1 = new Date();
var d2 = new Date(d1);
console.log(d1 == d2); // prints false (wrong!)
console.log(d1 === d2); // prints false (wrong!)
console.log(d1 != d2); // prints true (wrong!)
console.log(d1 !== d2); // prints true (wrong!)
console.log(d1.getTime() === d2.getTime()); // prints true (correct)
不过,我建议您使用下拉列表或类似的受约束的日期输入形式,而不是文本框,以免您陷入输入验证地狱。
对于好奇的date.getTime()文档:
返回指定日期的数值,即自1970年1月1日00:00:00 UTC以来的毫秒数。(之前的时间返回负值。)