有人能提出一种方法来比较两个大于、小于和过去不使用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;
}
}
}
}
}
其他回答
简短的回答
下面是一个函数,如果from dateTime>to dateTime Demo在操作中,则返回{boolean}
var from = '08/19/2013 00:00'
var to = '08/12/2013 00:00 '
function isFromBiggerThanTo(dtmfrom, dtmto){
return new Date(dtmfrom).getTime() >= new Date(dtmto).getTime() ;
}
console.log(isFromBiggerThanTo(from, to)); //true
解释
jsFiddle公司
var date_one = '2013-07-29 01:50:00',
date_two = '2013-07-29 02:50:00';
//getTime() returns the number of milliseconds since 01.01.1970.
var timeStamp_date_one = new Date(date_one).getTime() ; //1375077000000
console.log(typeof timeStamp_date_one);//number
var timeStamp_date_two = new Date(date_two).getTime() ;//1375080600000
console.log(typeof timeStamp_date_two);//number
因为您现在的日期时间都是数字类型您可以将它们与任何比较操作进行比较
(>,<,=,!=,==,!==,>=和<=)
Then
如果您熟悉C#自定义日期和时间格式字符串,这个库应该做完全相同的事情,并帮助您格式化日期和时间dtmFRM,无论您传递的是日期时间字符串还是unix格式
用法
var myDateTime = new dtmFRM();
alert(myDateTime.ToString(1375077000000, "MM/dd/yyyy hh:mm:ss ampm"));
//07/29/2013 01:50:00 AM
alert(myDateTime.ToString(1375077000000,"the year is yyyy and the day is dddd"));
//this year is 2013 and the day is Monday
alert(myDateTime.ToString('1/21/2014', "this month is MMMM and the day is dd"));
//this month is january and the day is 21
DEMO
您所要做的就是传递库js文件中包含的任何格式
假设您处理的是2014年[:-/.]06[:-//.]06或2014年06月[:-/./.]06日格式,那么您可以这样比较日期
var a = '2014.06/07', b = '2014-06.07', c = '07-06/2014', d = '07/06.2014';
parseInt(a.replace(/[:\s\/\.-]/g, '')) == parseInt(b.replace(/[:\s\/\.-]/g, '')); // true
parseInt(c.replace(/[:\s\/\.-]/g, '')) == parseInt(d.replace(/[:\s\/\.-]/g, '')); // true
parseInt(a.replace(/[:\s\/\.-]/g, '')) < parseInt(b.replace(/[:\s\/\.-]/g, '')); // false
parseInt(c.replace(/[:\s\/\.-]/g, '')) > parseInt(d.replace(/[:\s\/\.-]/g, '')); // false
如您所见,我们去掉分隔符,然后比较整数。
var date = new Date(); // will give you todays date.
// following calls, will let you set new dates.
setDate()
setFullYear()
setHours()
setMilliseconds()
setMinutes()
setMonth()
setSeconds()
setTime()
var yesterday = new Date();
yesterday.setDate(...date info here);
if(date>yesterday) // will compare dates
以上给出的所有答案只解决了一件事:比较两个日期。
事实上,它们似乎是这个问题的答案,但其中很大一部分是缺失的:
如果我想检查一个人是否满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.js库。
Date.compare(Date-date1,Date-date2)方法,返回一个数字,表示以下结果之一:
-1=日期1小于日期2。0=值相等。1=日期1大于日期2。