Jump to content

Set Default Date If Not Entered/calculations Based On That Date


Recommended Posts

I have a submission page that will calculate the Week number QTR and YR based on tomorrows date. I would like to be able to allow entry into the DeliveryDate field and use that date for calculations while defaulting to tomorrow if left blank.  My working javascript for tomorrow is below.

<SCRIPT LANGUAGE="JavaScript">
Date.prototype.getWeekNumber = function(){
    var d = new Date(+this);
    d.setHours(0,0,0);
    d.setDate(d.getDate()+4-(d.getDay()||7));
    return Math.ceil((((d-new Date(d.getFullYear(),0,1))/8.64e7)+1)/7);
};

 function calculate()
 {

var today = new Date(+new Date() + 86400000);

var dd = today.getDate();
var mm = today.getMonth()+1;
var yyyy = today.getFullYear();
var wk = today.getWeekNumber(yyyy,mm,dd);
var qtr = 1;

if (wk>52){qtr=1}
else if(wk<=13){qtr=1}
else if (wk<=26){qtr=2}
else if (wk<=39){qtr=3}
else {qtr=4}

document.getElementById("InsertRecordDeliveryDate").value = mm+'/'+dd+'/'+yyyy;
document.getElementById("InsertRecordYR").value = yyyy;
document.getElementById("InsertRecordWK").value = wk;
document.getElementById("InsertRecordQTR").value = qtr;

 }

 /* On submitting the webform, the function calculate is executed */
 document.getElementById("caspioform").onchange=calculate;

</SCRIPT>

Any help is appreciated.

 

Thanks

Link to comment
Share on other sites

  • 2 weeks later...

Hi ychaudhry,

 

How are you? How's the weather in your city?

 

I rewrite your code a bit:

<SCRIPT LANGUAGE="JavaScript">
function getWeekNumber()
{
   var d = new Date();
   var entered = document.getElementById("InsertRecordDeliveryDate").value;
    if (entered.length > 0)
        d = new Date(Date.parse(entered));
   d.setHours(0,0,0);
   d.setDate(d.getDate()+4-(d.getDay()||7));
   return Math.ceil((((d-new Date(d.getFullYear(),0,1))/8.64e7)+1)/7);
}

function calculate()
  {
    var today = new Date();
    var entered = document.getElementById("InsertRecordDeliveryDate").value;
    if (entered.length > 0)
        today = new Date(Date.parse(entered));
    var dd = today.getDate();
    var mm = today.getMonth()+1;
    var yyyy = today.getFullYear();
    var wk = getWeekNumber();
    var qtr = 1;

    if (wk>52){qtr=1}
      else if(wk<=13){qtr=1}
      else if (wk<=26){qtr=2}
      else if (wk<=39){qtr=3}
      else {qtr=4}

    document.getElementById("InsertRecordDeliveryDate").value = mm+'/'+dd+'/'+yyyy;
    document.getElementById("InsertRecordYR").value = yyyy;
    document.getElementById("InsertRecordWK").value = wk;
    document.getElementById("InsertRecordQTR").value = qtr;
}

 /* On submitting the webform, the function calculate is executed */
document.onload = calculate();
document.getElementById("InsertRecordDeliveryDate").onchange=calculate;


</SCRIPT>

Change the name of the field ("InsertRecordDeliveryDate") to the your name of the field in both cases.

 

I'll be grateful, if you tell me if the code works.

Have a nice day!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...