Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-20-2005, 04:22 AM   PM User | #1
tanpl3
New Coder

 
Join Date: Oct 2004
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
tanpl3 is an unknown quantity at this point
date calculation

i want to calculate the expiry date of my loan's
so when i key in a original loan's date, for example, (dmy) 01/01/2002, loan tenor would be 12 month or 1 year or 365 day, my loan's expiry date should be 31/12/2002, but somehow the return value is 01/01/2003.

please please to help :
tanpl3 is offline   Reply With Quote
Old 01-20-2005, 04:24 AM   PM User | #2
Brandoe85
teh Moderatorinator


 
Join Date: Sep 2004
Location: USA
Posts: 2,472
Thanks: 4
Thanked 40 Times in 40 Posts
Brandoe85 will become famous soon enough
Can you post your code?
__________________
-Brando
Why using tables for eating is stupid!
Brandoe85 is offline   Reply With Quote
Old 01-20-2005, 08:16 AM   PM User | #3
tanpl3
New Coder

 
Join Date: Oct 2004
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
tanpl3 is an unknown quantity at this point
actually this not my code, i get from internet

<html>

<head>

<title>New Page 1</title>
</head>
<script language="javascript">
function isLeapYear(year) {
if (year % 4 == 0) {
if (year % 100 == 0) {
return (year % 400 == 0);
} else {
return (true);
}
}
return (false);
}

function daysInMonth(month, year) {
switch (month) {
case 0: return 31; // january
case 1: // february
if (isLeapYear(year)) {
return 29;
} else {
return 28;
}
case 2: return 31; // march
case 3: return 30; // april
case 4: return 31; // may
case 5: return 30; // june
case 6: return 31; // july
case 7: return 31; // august
case 8: return 30; // september
case 9: return 31; // october
case 10: return 30; // november
case 11: return 31; // december
}
}

function DateAdd(datepart, number, date) {
var d = new Date(date);
var monthname=new Array("01","02","03","04","05","06","07","08","09","10","11","12");

switch (datepart) {
// day
case "d":
case "dd":
case "D":
tmpdt= (new Date(Date.parse(d) + ((number*1000*60*60*24)-1)));
tmpdt =(tmpdt.getDate() + "/" + monthname[tmpdt.getMonth()] + "/" + tmpdt.getFullYear());
return tmpdt

// month
case "m":
case "M":
case "mm":
var i = 0;
var maxcurr = daysInMonth(d.getMonth(), d.getFullYear());
var mm = (d.getMonth() + number) % 12;
if (mm < 0) mm += 12;
var yy = d.getFullYear() + Math.floor((number + d.getMonth()) / 12);
var maxnext = daysInMonth(mm, yy);
if (maxnext < d.getDate()) {
i = (maxnext - d.getDate());
}
if (d.getDate() == maxcurr) {
i = (maxnext - maxcurr);
}

tmpdt= new Date(d.getFullYear(), d.getMonth()+number,d.getDate()+i);
tmpdt =(tmpdt.getDate() + "/" + monthname[tmpdt.getMonth()] + "/" + tmpdt.getFullYear());
Date(d.getFullYear(),d.getMonth()+number,d.getDate()+i,d.getHours(),d.getMinutes(),d.getSeconds());
return tmpdt

// year
case "y":
case "yy":
case "yyyy":
var i = 0;
if (d.getMonth() == 1) {
if (d.getDate() == 29) {
if (!isLeapYear(d.getFullYear() + number)) {
i = -1;
}
}
if (d.getDate() == 28) {
if (!isLeapYear(d.getFullYear())) {
if (isLeapYear(d.getFullYear() + number)) {
i = 1;
}
}
}
}
tmpdt = parseInt(d.getFullYear()) + parseInt(number)
tmpdt = new Date(tmpdt,d.getMonth(),d.getDate()+i);
tmpdt =(tmpdt.getDate() + "/" + monthname[tmpdt.getMonth()] + "/" + tmpdt.getFullYear());
return tmpdt
}
}

function pasvalue(k, t)
{
var strSeperator = "/";
cdt = k.value;
var mDay = cdt.substr(0,2);
var mMonth = cdt.substr(3,2);
var mYear = cdt.substr(6,4)
ncdt = mMonth+strSeperator+mDay+strSeperator+mYear;
alert (ncdt)

dpt = t.T2.value;
dno = t.T1.value;

DateAdd(dpt, dno, ncdt)
document.t1.T4.value=tmpdt;
}

</script>
<body>

<form method="POST" action="--WEBBOT-SELF--" name="t1">
<p>
<input type="text" name="T1" size="3" tabindex="1">
<input type="text" name="T2" size="1" tabindex="2">
<input type="text" name="T3" size="10" onblur="pasvalue(this, document.t1)" ></p>
<input type="text" name="T4" size="53" tabindex="4">
<p><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>

</body>

</html>
tanpl3 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:44 AM.


Advertisement
Log in to turn off these ads.