Focal Point
[CLOSED] Dates difference in days javascript

This topic can be found at:
https://forums.informationbuilders.com/eve/forums/a/tpc/f/7971057331/m/2097069526

November 07, 2012, 12:19 PM
BI_Developer
[CLOSED] Dates difference in days javascript
Hi

I have 2 calendar controls in YYMD format and I am trying to find difference in days so that I could alert if date range is more than 60 days.

I am using this logic.
var dt_1 = document.form1.DT1.value;
var dt_2 = document.form1.DT2.value;

var no_of_days = dt_2 - dt_1;

if (dt_1 == '' && dt_2 == '')
{
	alert("Please enter a range for Date");
}

if (dt_1 > dt_2)
{
	alert("FROM date cannot be greater than TO date");
}

alert(no_of_days);


Everything works but I dont get the number of days. It shows 'NaN' in the alert box when I try to get value in no_of_days.
Can someone help me on this?

Thank you

This message has been edited. Last edited by: Kerry,


WF 8.2.01 APP STUDIO
PDF,HTML,EXL2K,Active
November 07, 2012, 02:13 PM
Francis Mariani
You really should try Google, 'javascript difference between two dates' -should provide links to the answer. Most likely the DT1 control is a text box, hence it's alphanumeric and alphanumeric - alphanumeric will most likely lead to NaN - the NaN property represents "Not-a-Number" value.

You need to convert the value of these text boxes to JavaScript date variables and then subtract, but Google will lead you to this.

Bonne chance.


Francis


Give me code, or give me retirement. In FOCUS since 1991

Production: WF 7.7.05M, Dev Studio, BID, MRE, WebSphere, DB2 / Test: WF 8.1.05M, App Studio, BI Portal, Report Caster, jQuery, HighCharts, Apache Tomcat, MS SQL Server
November 07, 2012, 02:20 PM
BI_Developer
I tried few ways but doesn't work.
quote:
Originally posted by Francis Mariani:
You really should try Google, 'javascript difference between two dates' -should provide links to the answer. Most likely the DT1 control is a text box, hence it's alphanumeric and alphanumeric - alphanumeric will most likely lead to NaN - the NaN property represents "Not-a-Number" value.

You need to convert the value of these text boxes to JavaScript date variables and then subtract, but Google will lead you to this.

Bonne chance.



WF 8.2.01 APP STUDIO
PDF,HTML,EXL2K,Active
November 07, 2012, 02:39 PM
Francis Mariani
How hard did you try?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Document </title>

<script type="text/javascript">

function Calc1()
{
var dt_1 = new Date(document.getElementById('DT1').value);
var dt_2 = new Date(document.getElementById('DT2').value);

//Set 1 day in milliseconds
var one_day = 1000 * 60 * 60 * 24;

//Calculate difference between the two dates, and convert to days
var no_of_days = (dt_2 - dt_1) / (one_day);

if (dt_1 == '' && dt_2 == '')
{
	alert("Please enter a range for Date");
}

if (dt_1 > dt_2)
{
	alert("FROM date cannot be greater than TO date");
}

alert(no_of_days);
}
</script>
</head>

<body>
<input id="DT1" type=text value="2010/10/23">
<input id="DT2" type=text value="2010/10/24">

<input type="button" onClick="Calc1();" value="Calculate">
</body>
</html>



Francis


Give me code, or give me retirement. In FOCUS since 1991

Production: WF 7.7.05M, Dev Studio, BID, MRE, WebSphere, DB2 / Test: WF 8.1.05M, App Studio, BI Portal, Report Caster, jQuery, HighCharts, Apache Tomcat, MS SQL Server
November 07, 2012, 03:47 PM
Waz
As Francis has stated in his example, the key is to condert the date text in the control to a javascript date by using the new Date() command.


Waz...

Prod:WebFOCUS 7.6.10/8.1.04Upgrade:WebFOCUS 8.2.07OS:LinuxOutputs:HTML, PDF, Excel, PPT
In Focus since 1984
Pity the lost knowledge of an old programmer!

November 07, 2012, 04:48 PM
Francis Mariani
And I should have stated that the difference between two dates is returned in milliseconds. To find out how many days, divide by the number of milliseconds in one day.


Francis


Give me code, or give me retirement. In FOCUS since 1991

Production: WF 7.7.05M, Dev Studio, BID, MRE, WebSphere, DB2 / Test: WF 8.1.05M, App Studio, BI Portal, Report Caster, jQuery, HighCharts, Apache Tomcat, MS SQL Server