Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    javascript date Math - date in past plus 14 days?

    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.

  • #2
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    example of what I've tried:

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

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    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;

    Code:
    <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.
    Last edited by Philip M; 12-14-2010 at 08:56 AM.

  • #4
    Regular Coder
    Join Date
    Nov 2009
    Posts
    247
    Thanks
    4
    Thanked 22 Times in 22 Posts
    swanvideos:

    Try this. Change the value of dueDate or billDate.

    Code:
    <!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>

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,986
    Thanks
    79
    Thanked 4,432 Times in 4,397 Posts
    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.
    Code:
    <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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #7
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    this ended up working for me:

    Code:
    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.

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by Old Pedant View Post
    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
    Code:
    var dueDateExist = this.Field("dueDate").Text;
    var billDateExist = this.Field("billDate").Text;
    works???????
    Last edited by Philip M; 12-17-2010 at 08:37 AM.

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,986
    Thanks
    79
    Thanked 4,432 Times in 4,397 Posts
    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?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #10
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •