/    Sign up×
Community /Pin to ProfileBookmark

Add xdays to date

Hi all
Trying to add 13 weeks or 91 days to a user entered date.
I’ve got an onBlur event on the field in question, but i’m not having much joy with the calc. Can anyone assist.

Thanks.

<script type=”text/javascript”>
// ———————————————————
// Function to Decision Due Date based on 13 weeks from
// Date Registered (dd/mm/yy)
// ———————————————————
function doCalcDecDue() {
// retrieve values
vardoc = document.forms[“FormData”];
var dateReg = new Date();
dateReg = vardoc.txtPlanReg.value;
// perform calculations
calcDate = dateReg + 91;
// update density field
vardoc.txtPlanDue.value = calcDate;
}
// ———————————————————
</script>

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@MjhLkwdSep 21.2005 — The code below does not require an input, but will add 91 days to a valid input, which may be in yyyy or yy format:

[CODE]<HTML>
<Head>
<Script type="text/javascript">

function validate(isField){

if (isField.value == ""){return}
splitDate = isField.value.split("/");
if (splitDate[2] && splitDate[2].length == 2){splitDate[2] = "20"+splitDate[2]}
refDate = new Date(splitDate[1]+"/"+splitDate[0]+"/"+splitDate[2]);
if (splitDate[1] < 1 || splitDate[1] > 12 || refDate.getDate() != splitDate[0] || splitDate[2].length != 4 || (!/^20/.test(splitDate[2])))
{
alert('Invalid date');
isField.value = "";
isField.focus();
}
else {
dateStr = new Date(refDate.getFullYear(),refDate.getMonth(),refDate.getDate()+91);
slashStr = dateStr.getDate()+"/"+(dateStr.getMonth()+1)+"/"+dateStr.getFullYear();
isField.value = slashStr;
}
}

</Script>
</Head>
<Body>
<Form name='Form1'>
Assumes that the year begins with 20
<br>
Adds 91 days to a valid input date
<br>
Some Date: (dd/mm/yyyy) or (dd/mm/yy)<input type='text' size='9' name='nDate' onblur="validate(this)"><br>
Nothing: <input type='text' size='9'>
</Form>
</Body>
</HTML>[/CODE]


I tested the other code posted four entries below with:

4/7/000

32/12/2

and the output was in error for each.
Copy linkTweet thisAlerts:
@CharlesSep 21.2005 — &lt;label&gt;Date &lt;input onchange="var d = new Date (this.value); d.setDate (d.getDate() + 91); this.value = d.toDateString()" type="text"&gt;&lt;/label&gt;
Copy linkTweet thisAlerts:
@BigJohnsonauthorSep 22.2005 — MjhLkwd

That seems perfect. Can the year check be made to account for "yy" &/or "yyyy" years? This allows the user the ultimate input flexibility.

Many thanks.!
Copy linkTweet thisAlerts:
@BigJohnsonauthorSep 22.2005 — Hi again,

I read your other post regarding the yy & yyyy question and have sorted that now. Many thanks for your assistance - it works great!

A final question, can the script be mod'd to handle null field values? eg, as I tab around the form, if I enter the ref1 field and try to leave without entering anything, the invalid date error occurs.

Many thanks.
Copy linkTweet thisAlerts:
@CharlesSep 22.2005 — &lt;script type="text/javascript"&gt;

<i> </i>Date.fromDDMMYY = function (s) {return (/^(dd?)D(dd?)D(d{2})$/).test(s) ? new Date('20' + RegExp.$3, RegExp.$2 - 1, RegExp.$1) : new Date (s)}

<i> </i>Date.fromDDMMYYYY = function (s) {return (/^(dd?)D(dd?)D(d{4})$/).test(s) ? new Date(RegExp.$3, RegExp.$2 - 1, RegExp.$1) : Date.fromDDMMYY (s)}

<i> </i>Date.prototype.toDDMMYYYYString = function () {return isNaN (this) ? 'NaN' : [this.getDate() &gt; 9 ? this.getDate() : '0' + this.getDate(), this.getMonth() &gt; 8 ? this.getMonth() + 1 : '0' + (this.getMonth() + 1), this.getFullYear()].join('/')}

&lt;/script&gt;
&lt;label&gt;Date &lt;input onchange="if (this.value) {var d = Date.fromDDMMYYYY (this.value); d.setDate (d.getDate() + 91); this.value = d.toDDMMYYYYString()}" type="text"&gt;&lt;/label&gt;
Copy linkTweet thisAlerts:
@BigJohnsonauthorSep 22.2005 — Many thanks Charles. I saw that you were using the onChange function as opposed to the onBlur function - works perfectly - GREAT!
×

Success!

Help @BigJohnson spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...