maqskywalker
asked on
comparing a variable containing a date string in JavaScript
I have a regular html page with some JavaScript on a button click event.
This is my current code on my page:
When I click the button, In the if statement this, variable iscurrentfiscalyearvar currently returns "false"
but when FiscalYear is 2019 it should return true.
I think i need my issue is in this line.
if (fiscalstartvar <= vartodaysdate && fiscalendvar >= vartodaysdate) {
Since those variables hold strings that are dates in order to do a less than equal to comparison do i have to convert them to datetype somehow.
Anyone know the syntax or how I fix my example?
This is my current code on my page:
<!DOCTYPE html>
<html>
<head>
<title>Test Page</title>
<style type="text/css">
body {
padding: 30px;
font-family: Arial;
}
</style>
<script type="text/javascript">
function TestFunction() {
// fiscalstartvar is 07/01/FiscalYear-1
// fiscalendvar is 06/30/FiscalYear
// save value of fiscalyear hidden field in variabl
var fiscalyearvar = 2019;
var vartodaysdate = "09/16/2018";
var fiscalyearminusonevar = fiscalyearvar - 1;
var fiscalstartvar = "07/01/" + fiscalyearminusonevar;
var fiscalendvar = "06/30/" + fiscalyearvar;
if (fiscalstartvar <= vartodaysdate && fiscalendvar >= vartodaysdate) {
var iscurrentfiscalyearvar = "true";
}
else {
var iscurrentfiscalyearvar = "false";
}
// display on div
document.getElementById("DivIsCurrentFiscalYearVar").innerHTML = iscurrentfiscalyearvar;
}
</script>
</head>
<body>
<input id="Button1" type="button" value="Button1" onclick="TestFunction();"/>
<br />
<Div id="DivIsCurrentFiscalYearVar"></Div>
</body>
</html>
When I click the button, In the if statement this, variable iscurrentfiscalyearvar currently returns "false"
but when FiscalYear is 2019 it should return true.
I think i need my issue is in this line.
if (fiscalstartvar <= vartodaysdate && fiscalendvar >= vartodaysdate) {
Since those variables hold strings that are dates in order to do a less than equal to comparison do i have to convert them to datetype somehow.
Anyone know the syntax or how I fix my example?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER