有人能提出一种方法来比较两个大于、小于和过去不使用JavaScript的日期的值吗?值将来自文本框。
当前回答
另一种比较两个日期的方法是通过toISOString()方法。这在与保存在字符串中的固定日期进行比较时特别有用,因为可以避免创建短时间对象。借助ISO 8601格式,您可以按字典方式比较这些字符串(至少在使用相同时区时)。
我不一定说它比使用时间对象或时间戳更好;只是将此作为另一种选择。这可能会失败,但我还没有发现:)
其他回答
假设您处理的是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
如您所见,我们去掉分隔符,然后比较整数。
function compare_date(date1, date2){
const x = new Date(date1)
const y = new Date(date2)
function checkyear(x, y){
if(x.getFullYear()>y.getFullYear()){
return "Date1 > Date2"
}
else if(x.getFullYear()<y.getFullYear()){
return "Date2 > Date1"
}
else{
return checkmonth(x, y)
}
}
function checkmonth(x, y){
if(x.getMonth()>y.getFullYear()){
return "Date1 > Date2"
}
else if(x.getMonth()<y.getMonth){
return "Date2 > Date1"
}
else {
return checkDate(x, y)
}
}
function checkDate(x, y){
if(x.getDate()>y.getFullYear()){
return "Date1 > Date2"
}
else if(x.getDate()<y.getDate()){
return "Date2 > Date1"
}
else {
return checkhour(x,y)
}
}
function checkhour(x, y){
if(x.getHours()>y.getHours()){
return "Date1 > Date2"
}
else if(x.getHours()<y.getHours()){
return "Date2 > Date1"
}
else {
return checkhmin(x,y)
}
}
function checkhmin(x,y){
if(x.getMinutes()>y.getMinutes()){
return "Date1 > Date2"
}
else if(x.getMinutes()<y.getMinutes()){
return "Date2 > Date1"
}
else {
return "Date1 = Date2"
}
}
return checkyear(x, y)
我通常将日期作为时间戳(数字)存储在数据库中。
当我需要比较时,我只需在这些时间戳或
将其转换为Date Object,然后根据需要与><进行比较。
请注意,==或==无法正常工作,除非变量是同一日期对象的引用。
首先将这些Date对象转换为时间戳(数字),然后比较它们的相等性。
日期到时间戳
var timestamp_1970 = new Date(0).getTime(); // 1970-01-01 00:00:00
var timestamp = new Date().getTime(); // Current Timestamp
截至日期的时间戳
var timestamp = 0; // 1970-01-01 00:00:00
var DateObject = new Date(timestamp);
减去两个日期,得到以毫秒为单位的差值,如果得到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>
铬的结果