...

View Full Version : javascript date Math - date in past plus 14 days?



swanvideos
12-14-2010, 12:37 AM
Not sure if this is possible in javascript:

I'm looking for two different dates (bill date and due date) on an invoice that are captured by OCR. If one of them exists, but the other does not, I want the empty field to be 14 days before (or after) the other.

For example:
if the bill date is 7/27/2010 and the due date was not captured, I want to set the due date as 8/10/2010 (14 days after the bill date).

If the due date was captured as 8/10/2010, but the due date is blank, I want to assign the bill date as 7/27/2010 (14 days before the due date).

if both dates have values, do nothing.

Thanks.

swanvideos
12-14-2010, 12:38 AM
example of what I've tried:



var d= new Date(Field("billDate").Value);
var endDueDate = d.Value +(1000*60*60*24*7);
Field("dueDate").Text = endDueDate.Value;

Philip M
12-14-2010, 09:09 AM
Here you are. This may be more elaborate than you need and you may not want the date checking or add suffix functions. Naturally you can configure the output as you wish.

You should remember that Javascript is case-sensitive and Value is not the same as value. And I have to say that your Javascript syntax is rather muddled.
Field("dueDate").Text = endDueDate.Value;
should be something like
document.formname.dueDate.value = endDueDate;



<script type="text/javascript">

var flag;
function addDaysToDate(yr, mmx, dd) {
var daysAhead = 14;
var months=['January','February','March','April','May','June','July','August','September','October','November',' December']
var mm = mmx-1;
checkValidDate(yr,mmx,dd);
if (!flag) {return false}

var nd = new Date(yr, mm, dd); // YYYY/MM/DD - months in Javascript are 0-11
nd.setMonth(nd.getMonth());
nd.setDate(nd.getDate() + daysAhead);
var month = nd.getMonth();
var day = nd.getDate();
var year = nd.getFullYear();
getSuffix(day);
futureDate = (day + daySuffix + " " + months[month] + " " + year);
alert (futureDate);
document.write(futureDate);
}

function getSuffix(dd) {
daySuffix = 'th';
if(dd != 11 && dd != 12 && dd != 13) { var ddx = dd % 10 }
else {var ddx = dd}
if (ddx == 1) { daySuffix = 'st' }
if (ddx == 2) { daySuffix = 'nd' }
if (ddx == 3) { daySuffix = 'rd' }
return daySuffix;
}

function checkValidDate(yr,mmx,dd) {
mm = mmx-1; // remember that in Javascript date objects the months are 0-11
var nd = new Date();
nd.setFullYear(yr,mm,dd);
var ndmm = nd.getMonth();
if (ndmm != mm) {
alert (dd + "/" + mmx + "/" + yr + " is an Invalid Date!");
flag = false;
}
else {
alert (dd + "/" + mmx + "/" + yr + " is a Valid Date");
flag = true;
}
}

addDaysToDate(2010, 12, 16); // 16th December 2010
</script>


To get 14 days before, simply change daysAhead from 14 to -14.

Sciliano
12-14-2010, 03:08 PM
swanvideos:

Try this. Change the value of dueDate or billDate.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>None</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">

function init(){

var nForm = document.forms[0];
var nBillDate = nForm['billDate'].value == "" ? false : new Date(nForm['billDate'].value);
var nDueDate = nForm['dueDate'].value == "" ? false : new Date(nForm['dueDate'].value);
var nOffset = nBillDate && nDueDate ? true : nBillDate && !nDueDate ? 14 : !nBillDate && nDueDate ? - 14 : false;
if (!nOffset)
{
alert('Error, Bill Date and Due Date are empty');
return;
}
if (nOffset == true)
{
return;
}
var refDate = nOffset == 14 ? new Date(nForm['billDate'].value) : new Date(nForm['dueDate'].value);
var offsetDate = new Date(refDate.getFullYear(),refDate.getMonth(),refDate.getDate()+nOffset);
if (nBillDate)
{
nForm['dueDate'].value = offsetDate.getMonth()+1+"/"+offsetDate.getDate()+"/"+offsetDate.getFullYear();
}
else {
nForm['billDate'].value = offsetDate.getMonth()+1+"/"+offsetDate.getDate()+"/"+offsetDate.getFullYear();
}
}

navigator.appName == "Microsoft Internet Explorer" ? attachEvent('onload', init, false) : addEventListener('load', init, false);

</script>
<style type="text/css">

body {background-color: #fffacd; margin-top: 60px;}

</style>
</head>
<body>
<form action="" method="post">

<label>Bill Date:&nbsp;&nbsp;<input type="text" name="billDate" size="9" readonly></label>
<br>
<label>Due Date: <input type="text" name="dueDate" size="9" value="8/10/2010" readonly></label>
<br>
<input type="submit" name="submit" value="Submit">

</form>
</body>
</html>

Old Pedant
12-14-2010, 08:56 PM
Ummm...he said the info is captured by an OCR reader, which means it is captured *AFTER* the page is loaded (and presumably before the form is submitted).

So you would want to attach the code to the <form>'s onsubmit, not to the page's onload.

It seems to me that both Philip and Sciliano are over-complicating this.


<html>
<head>
<title>Zambonis Rule</title>
<script type="text/javascript">
function dcheck(form) {
var due = new Date(form.dueDate.value);
var bill = new Date(form.billDate.value);
if ( isNaN(due) ) {
if ( isNaN(bill) ) {
alert("Neither dueDate nor billDate given!");
return false;
}
due = new Date(bill.getFullYear(),bill.getMonth(),bill.getDate()+14);
} else if ( isNaN(bill) ) {
bill = new Date(due.getFullYear(),due.getMonth(),due.getDate()-14);
}
form.dueDate.value = due.getMonth()+1 + "/" + due.getDate() + "/" + due.getFullYear();
form.billDate.value = bill.getMonth()+1 + "/" + bill.getDate() + "/" + bill.getFullYear();
return true;
}
</script>
<style type="text/css">

body {background-color: #fffacd; margin-top: 60px;}

</style>
</head>
<body>
<form action="" method="get" onsubmit="return dcheck(this);">
<label>Bill Date: <input type="text" name="billDate"></label>
<br>
<label>Due Date: <input type="text" name="dueDate""></label>
<br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>

If you can't stand seeing "onXXX" coded into <form>s, then attach the handler a la Sciliano, but to the form's onsubmit, not to the page's onload.

swanvideos
12-16-2010, 09:57 PM
I guess I should clarify that this is NOT for use on a webpage. It's javascript used in a piece of desktop software. I'll try your suggestions and let you know how it goes. Thanks for your help.

swanvideos
12-17-2010, 01:07 AM
this ended up working for me:



var dueDateExist = this.Field("dueDate").Text;
var billDateExist = this.Field("billDate").Text;

if (!dueDateExist && billDateExist)
{
var billYear = billDateExist.replace(/(\d\d)[/](\d\d)[/](\d\d\d\d)/i, "$3");
var billMonth = billDateExist.replace(/(\d\d)[/](\d\d)[/](\d\d\d\d)/i, "$1");
var billDate = billDateExist.replace(/(\d\d)[/](\d\d)[/](\d\d\d\d)/i, "$2");
billMonth = billMonth-1;
var myDate=new Date();
myDate.setFullYear(billYear,billMonth,billDate);
myDate.setDate(myDate.getDate()+14); //date is moved 14 days into future
this.Field("dueDate").Text = myDate;
}

if (!billDateExist && dueDateExist)
{
var dueYear = dueDateExist.replace(/(\d\d)[/](\d\d)[/](\d\d\d\d)/i, "$3");
var dueMonth = dueDateExist.replace(/(\d\d)[/](\d\d)[/](\d\d\d\d)/i, "$1");
var dueDate = dueDateExist.replace(/(\d\d)[/](\d\d)[/](\d\d\d\d)/i, "$2");
dueMonth = dueMonth-1;
var myDate=new Date();
myDate.setFullYear(dueYear,dueMonth,dueDate);
myDate.setDate(myDate.getDate()-14); //date is moved 14 days into future
this.Field("billDate").Text = myDate;
}


Feel free to make fun of my code or regex :).

Thanks for your suggestions.

Philip M
12-17-2010, 09:35 AM
It seems to me that both Philip and Sciliano are over-complicating this.


Hmmm.... Have you tried entering silly date values into your script, or filling in both boxes?

swanvidoes - you say that

var dueDateExist = this.Field("dueDate").Text;
var billDateExist = this.Field("billDate").Text;
works???????

Old Pedant
12-17-2010, 07:39 PM
Yeah, Philip. Like he said, it's not in a browser. I'm guessing it's in some Windows or maybe Flash program.

"this" probably refers to the form/window and ".Text" is probably a legit property.

Who knows?

swanvideos
12-17-2010, 08:38 PM
Yeah, Philip. Like he said, it's not in a browser. I'm guessing it's in some Windows or maybe Flash program.

"this" probably refers to the form/window and ".Text" is probably a legit property.

Who knows?

exactly. I'm using ABBYY FlexiCapture and the properties are valid. This script I posted does exactly what I wanted it to do. I understand this would NOT work in a web browser. Thanks again for the tips.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum