我想在javascript中比较3个日期 [英] I want to compare 3 dates in javascript
本文介绍了我想在javascript中比较3个日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在javascript中有3个日期
1)
var vDate = document.getElementById('AppliedFor'+ RowNo).value;
它的日期为
25 / May / 2018
2)
var HDNSessionFromDate1 = document.getElementById('HDNSessionFromDate')。value;
它给我的日期为
01 / Apr / 2018
3)
var HDNSessionToDate = document.getElementById('HDNSessionToDate' ).value;
它给出的日期为
31 / Mar / 2019
现在我要检查我的日期
vDate应介于< pre> HDNSessionFromDate1
和
HDNSessionToDate $之间p $ p>
我尝试过:
我在javascript中有3个日期
1)< pre> var vDate = document.getElementById('AppliedFor'+ RowNo).value;
它的日期为25 / May / 2018
2)var HDNSessionFromDate1 = document.getElementById('HDNSessionFromDate')。value;
它给我的日期为01 / Apr / 2018
3)var HDNSessionToDate = document.getElementById('HDNSessionToDate')。value;
它给日期31 / Mar / 2019
现在我想查看我的日期vDate应介于< pre> HDNSessionFromDate1和
HDNSessionToDate
$之间b $ b
现在我将它转换为mm / dd / yyyy格式,因为我有功能
GetNewFormat()
var temp = GetNewFormat(vDate);
这里我的日期为05/25/2018
temp = temp.split(/);
var month = parseInt(temp [0]);
var day = parseInt(temp [1]);
var year = parseInt(temp [2]);
var temp1 = GetNewFormat(HDNSessionFromDate1);
这里我得到日期为04/01/2018
temp1 = temp1.split(/);
var month1 = parseInt(temp1 [0]);
var day1 = parseInt(temp1 [1]);
var year1 = parseInt(temp1 [2]);
var temp2 = GetNewFormat(HDNSessionToDate);
这里我得到日期为03/31/2019
temp2 = temp2.split(/);
var month2 = parseInt(temp2 [0]);
var day2 = parseInt(temp2 [1]);
var year2 = parseInt(temp2 [2]);
alert(year2);
var vDate1 =新日期(年,月,日);
alert(vDate1);
现在我有
var vDate1 =新日期(年,月,日);
var vDate2 =新日期(year1,month1,day1);
var vDate3 =新日期(year2,month2,day2);
alert(vDate1);
if(vDate1> = vDate2&& vDate1< = vDate3)
{
lblresult.innerHTML =请输入有效日期
lblresult.className ='红色';
document.getElementById('AppliedFor'+ RowNo).focus();
返回false;
}
但它给出了$ b $的错误b var vDate1 =新日期(年,月,日);
我创建的函数以下列格式给出日期
< pre>函数GetNewFormat(datevar){
var mon;
var m1 = datevar.substring(3,6);
switch(m1.toLowerCase()){
casejan:
mon = 01;
休息;
casefeb:
mon = 02;
休息;
casemar:
mon = 03;
休息;
caseapr:
mon = 04;
休息;
casemay:
mon = 05;
休息;
casejun:
mon = 06;
休息;
casejul:
mon = 07;
休息;
caseaug:
mon = 08;
休息;
casesep:
mon = 09;
休息;
caseoct:
mon = 10;
休息;
casenov:
mon = 11;
休息;
casedec:
mon = 12;
休息;
}
datevar = mon +/+ datevar.substring(0,3)+ datevar.substring(7);
return datevar;
}
解决方案
用于创建Date()对象的月份应为0-11而不是1-12。
JavaScript日期参考 [ ^ ]
I have 3 Dates in javascript
1)var vDate = document.getElementById('AppliedFor' + RowNo).value;
it gives date as"25/May/2018"
2)var HDNSessionFromDate1 = document.getElementById('HDNSessionFromDate').value;
it Gives me Date as"01/Apr/2018"
3)var HDNSessionToDate = document.getElementById('HDNSessionToDate').value;
it gives Date as"31/Mar/2019"
now I want To check my datevDate should be in between <pre>HDNSessionFromDate1and
HDNSessionToDate
What I have tried:
I have 3 Dates in javascript 1) <pre>var vDate = document.getElementById('AppliedFor' + RowNo).value;
it gives date as"25/May/2018"
2)var HDNSessionFromDate1 = document.getElementById('HDNSessionFromDate').value;
it Gives me Date as"01/Apr/2018"
3)var HDNSessionToDate = document.getElementById('HDNSessionToDate').value;
it gives Date as"31/Mar/2019"
now I want To check my datevDate should be in between <pre>HDNSessionFromDate1and
HDNSessionToDate
now I am converting it into mm/dd/yyyy format for that I have function
GetNewFormat()
var temp = GetNewFormat(vDate); here I am getting Date as 05/25/2018 temp = temp.split("/"); var month = parseInt(temp[0]); var day = parseInt(temp[1]); var year =parseInt(temp[2]); var temp1 = GetNewFormat(HDNSessionFromDate1); here I am getting Date as "04/01/2018" temp1 = temp1.split("/"); var month1 = parseInt(temp1[0]); var day1 = parseInt(temp1[1]); var year1 = parseInt(temp1[2]); var temp2 = GetNewFormat(HDNSessionToDate); here I am getting Date as "03/31/2019" temp2 = temp2.split("/"); var month2 =parseInt(temp2[0]); var day2 =parseInt(temp2[1]); var year2 =parseInt(temp2[2]); alert(year2); var vDate1 = new Date(year, month, day); alert(vDate1);
now I have
var vDate1 = new Date(year, month, day); var vDate2=new Date(year1, month1, day1); var vDate3=new Date(year2, month2, day2); alert(vDate1); if (vDate1 >= vDate2 && vDate1 <= vDate3) { lblresult.innerHTML = "Please Enter Valid Date" lblresult.className = 'Red'; document.getElementById('AppliedFor' + RowNo).focus(); return false; }
but It Gives error at var vDate1 = new Date(year, month, day); I created Function which gives date in following format <pre>function GetNewFormat(datevar) { var mon; var m1 = datevar.substring(3, 6); switch (m1.toLowerCase()) { case "jan": mon = 01; break; case "feb": mon = 02; break; case "mar": mon = 03; break; case "apr": mon = 04; break; case "may": mon = 05; break; case "jun": mon = 06; break; case "jul": mon = 07; break; case "aug": mon = 08; break; case "sep": mon = 09; break; case "oct": mon = 10; break; case "nov": mon = 11; break; case "dec": mon = 12; break; } datevar = mon + "/" + datevar.substring(0, 3) + datevar.substring(7); return datevar; }解决方案The month used to create a Date() object should be 0-11 instead of 1-12.
JavaScript Date Reference[^]
这篇关于我想在javascript中比较3个日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文