有人能提出一种方法来比较两个大于、小于和过去不使用JavaScript的日期的值吗?值将来自文本框。
当前回答
为了从JavaScript中的自由文本创建日期,您需要将其解析为Date对象。
您可以使用Date.parse(),它获取自由文本并尝试将其转换为新的日期,但如果您能够控制页面,我建议使用HTML选择框或日期选择器,例如YUI日历控件或jQueryUI日期选择器。
正如其他人所指出的,一旦你有了日期,你就可以使用简单的算术来减去日期,并将其转换为天数,方法是将数字(以秒为单位)除以一天中的秒数(60*60*24=86400)。
其他回答
关系运算符<<=>>=可用于比较JavaScript日期:
var d1 = new Date(2013, 0, 1);
var d2 = new Date(2013, 0, 2);
d1 < d2; // true
d1 <= d2; // true
d1 > d2; // false
d1 >= d2; // false
然而,等式运算符==!===!==无法用于比较(的值)日期,因为:
对于严格或抽象的比较,两个不同的对象永远不相等。只有当操作数引用同一对象时,比较对象的表达式才为真。
您可以使用以下任一方法比较相等的日期值:
var d1 = new Date(2013, 0, 1);
var d2 = new Date(2013, 0, 1);
/*
* note: d1 == d2 returns false as described above
*/
d1.getTime() == d2.getTime(); // true
d1.valueOf() == d2.valueOf(); // true
Number(d1) == Number(d2); // true
+d1 == +d2; // true
Date.getTime()和Date.valueOf()都返回自1970年1月1日00:00UTC以来的毫秒数。Number函数和一元+运算符都在幕后调用valueOf()方法。
减去两个日期,得到以毫秒为单位的差值,如果得到0,则是相同的日期
function areSameDate(d1, d2){
return d1 - d2 === 0
}
function datesEqual(a, b)
{
return (!(a>b || b>a))
}
日期比较:
var str1 = document.getElementById("Fromdate").value;
var str2 = document.getElementById("Todate").value;
var dt1 = parseInt(str1.substring(0,2),10);
var mon1 = parseInt(str1.substring(3,5),10);
var yr1 = parseInt(str1.substring(6,10),10);
var dt2 = parseInt(str2.substring(0,2),10);
var mon2 = parseInt(str2.substring(3,5),10);
var yr2 = parseInt(str2.substring(6,10),10);
var date1 = new Date(yr1, mon1, dt1);
var date2 = new Date(yr2, mon2, dt2);
if(date2 < date1)
{
alert("To date cannot be greater than from date");
return false;
}
else
{
alert("Submitting ...");
document.form1.submit();
}
以上给出的所有答案只解决了一件事:比较两个日期。
事实上,它们似乎是这个问题的答案,但其中很大一部分是缺失的:
如果我想检查一个人是否满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;
}
}
}
}
}
推荐文章
- 同步和异步编程(在node.js中)的区别是什么?
- 在d3.js中调整窗口大小时调整svg的大小
- 如何将两个字符串相加,就好像它们是数字一样?
- 绑定多个事件到一个监听器(没有JQuery)?
- 在JavaScript中将JSON字符串解析为特定对象原型
- 将字符串“true”/“false”转换为布尔值
- 单元测试:日期时间。现在
- 如何使用JavaScript代码获得浏览器宽度?
- event.preventDefault()函数在IE中无法工作
- indexOf()和search()的区别是什么?
- 错误:'types'只能在.ts文件中使用- Visual Studio Code使用@ts-check
- React-Native:应用程序未注册错误
- LoDash:从对象属性数组中获取值数组
- src和dist文件夹的作用是什么?
- jQuery UI对话框-缺少关闭图标