有人能提出一种方法来比较两个大于、小于和过去不使用JavaScript的日期的值吗?值将来自文本框。
当前回答
注意时区
javascript日期没有时区的概念。这是一个时刻(从大纪元开始计时),具有方便的函数,用于在“本地”时区中转换字符串。如果您想使用日期对象处理日期,正如这里的每个人所做的那样,您希望您的日期在所讨论的日期开始时表示UTC午夜。这是一个常见且必要的惯例,允许您处理日期,而不考虑创建日期的季节或时区。因此,您需要非常警惕地管理时区的概念,尤其是在创建午夜UTC日期对象时。
大多数时候,您都希望您的日期反映用户的时区。如果今天是你的生日,请单击。新西兰和美国的用户同时点击并获得不同的日期。在这种情况下,这样做。。。
// create a date (utc midnight) reflecting the value of myDate and the environment's timezone offset.
new Date(Date.UTC(myDate.getFullYear(),myDate.getMonth(), myDate.getDate()));
有时,国际可比性胜过本地准确性。在这种情况下,这样做。。。
// the date in London of a moment in time. Device timezone is ignored.
new Date(Date.UTC(myDate.getUTCYear(), myDate.getyUTCMonth(), myDate.getUTCDate()));
现在你可以像其他答案所建议的那样直接比较你的约会对象。
在创建时注意管理时区,在转换回字符串表示形式时还需要确保将时区排除在外。因此,您可以安全地使用。。。
到ISOString()getUTCxxx()getTime()//返回一个没有时间或时区的数字。.toLocaleDateString(“fr”,{timezone:“UTC”})//任何您想要的语言环境,但始终是UTC。
完全避免其他一切,特别是。。。
getYear()、getMonth()和getDate()
其他回答
减去两个日期,得到以毫秒为单位的差值,如果得到0,则是相同的日期
function areSameDate(d1, d2){
return d1 - d2 === 0
}
表演
今天2020.02.27我在MacOs High Sierra v10.13.6上对Chrome v80.0、Safari v13.0.5和Firefox 73.0.1上选择的解决方案进行了测试
结论
所有浏览器的解决方案d1==d2(D)和d1==d2(E)最快解决方案getTime(A)比valueOf(B)快(两者都是中速)解决方案F、L、N对于所有浏览器来说都是最慢的
细节
下面的代码片段中介绍了性能测试中使用的解决方案。您可以在您的机器上执行测试
函数A(d1,d2){return d1.getTime()==d2.getTime);}函数B(d1,d2){返回d1.valueOf()==d2.valueOf();}函数C(d1,d2){返回数字(d1)==数字(d2);}函数D(d1,d2){返回d1==d2;}函数E(d1,d2){返回d1==d2;}函数F(d1,d2){返回(!(d1>d2||d2>d1));}函数G(d1,d2){返回d1*1==d2*1;}函数H(d1,d2){返回+d1==+d2;}函数I(d1,d2){回来(+d1-+d2);}函数J(d1,d2){回来(d1-d2);}函数K(d1,d2){返回d1-d2==0;}函数L(d1,d2){回来((d1>d2)-(d1<d2));}函数M(d1,d2){return d1.getFullYear()==d2.getFullYear()&&d1.getDate()==d2.getDate&&d1.getMonth()==d2.getMont();}函数N(d1,d2){return(isFinite(d1.valueOf())&&isFinite!((d1>d2)-(d1<d2)):假);}//测试let过去=新日期('2002-12-24');//过去的let now=新日期('2020-02-26');//现在console.log('代码d1>d2 d1<d2 d1=d2')var log=(l,f)=>console.log(`${l}${f(现在,过去)}${f(过去,现在)}${f(现在、现在)}`);日志('A',A);日志('B',B);日志('C',C);日志('D',D);对数('E',E);日志('G',G);对数('H',H);日志('I',I);日志('J',J);log('K',K);对数('L',L);日志(M’,M);对数('N',N);p{颜色:红色}<p>此代码段仅显示已测试的解决方案(它本身不执行测试)</p>
铬的结果
以上给出的所有答案只解决了一件事:比较两个日期。
事实上,它们似乎是这个问题的答案,但其中很大一部分是缺失的:
如果我想检查一个人是否满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;
}
}
}
}
}
在比较Dates对象之前,请尝试将两者的毫秒设置为零,如Date.setMilliseconds(0);。
在某些情况下,Date对象是用javascript动态创建的,如果您继续打印Date.getTime(),您将看到毫秒数发生变化,这将阻止两个日期相等。
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;}
}
推荐文章
- 一元加/数字(x)和parseFloat(x)之间的区别是什么?
- angularjs中的compile函数和link函数有什么区别
- 删除绑定中添加的事件监听器
- 很好的初学者教程socket.io?
- 在Android应用程序中显示当前时间和日期
- 字符串不能识别为有效的日期时间“格式dd/MM/yyyy”
- HtmlSpecialChars在JavaScript中等价于什么?
- 如何转换日期时间?将日期时间
- React: 'Redirect'没有从' React -router-dom'中导出
- 如何在React中使用钩子强制组件重新渲染?
- 我如何使用Jest模拟JavaScript的“窗口”对象?
- 我如何等待一个承诺完成之前返回一个函数的变量?
- 在JavaScript中根据键值查找和删除数组中的对象
- 使嵌套JavaScript对象平放/不平放的最快方法
- 如何以及为什么'a'['toUpperCase']()在JavaScript工作?