Menu
Hi,
how to generate a end date (format: RRRR-MM-DD) from a given date and number of days (as a parameters)
for example: enddate = 2005-05-05 + 5 days (result must be 2005-05-10, but how to generate it with JS?)
thanks
<script type="text/javascript">
<!--
Date.prototype.toW3CDTF = function () {return [this.getFullYear(), this.getMonth() < 9 ? '0' + (this.getMonth() + 1) : this.getMonth() + 1, this.getDate() < 10 ? '0' + this.getDate() : this.getDate()].join ('-')}
Date.prototype.incrementByDays = function (n) {
this.setDate (this.getDate () + n);
return this;
}
alert (new Date().incrementByDays (5).toW3CDTF ())
// -->
</script>
tells that enddate.getYear() is not a function[/QUOTE]Strickly speaking Date.getYear() isn't a JavaScript function, not any more. Because of the Y2K problem it was officially removed from the language. Use Date.getFullYear() instead.
<script type="text/javascript">
<!--
Date.fromW3CDTF = function (s) {return (/^(d{4})-(dd?)-(dd?)$/).test(s) ? new Date(RegExp.$1, RegExp.$2 - 1, RegExp.$3) : new Date (s)}
Date.prototype.toW3CDTF = function () {return [this.getFullYear(), this.getMonth() < 9 ? '0' + (this.getMonth() + 1) : this.getMonth() + 1, this.getDate() < 10 ? '0' + this.getDate() : this.getDate()].join ('-')}
Date.prototype.incrementByDays = function (n) {
this.setDate (this.getDate () + n);
return this;
}
alert (new Date.fromW3CDTF('2005-05-09').incrementByDays (5).toW3CDTF ())
// -->
</script>
[code=php]alert (new Date.fromW3CDTF('start date').incrementByDays(5).toW3CDTF ())[/code]
[code=php]<script type="text/javascript">
<!--
Date.fromW3CDTF = function (s) {return (/^(d{4})-(dd?)-(dd?)$/).test(s) ? new Date(RegExp.$1, RegExp.$2 - 1, RegExp.$3) : new Date (s)}
Date.prototype.toW3CDTF = function () {return [this.getFullYear(), this.getMonth() < 9 ? '0' + (this.getMonth() + 1) : this.getMonth() + 1, this.getDate() < 10 ? '0' + this.getDate() : this.getDate()].join ('-')}
Date.prototype.incrementByDays = function (n) {
this.setDate (parseInt(this.getDate()) + n);
return this;
}
alert (new Date.fromW3CDTF('2005-05-09').incrementByDays (5).toW3CDTF ())
// -->
</script>[/code]
From ECMA 262It works just fine as I had it.
15.9.1.5 Date Number
A date number is identified by an integer in the range 1 through 31, inclusive.[/quote]
<script type="text/javascript">
<!--
alert (typeof (new Date().getDate()));
// -->
</script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head>
</head>
<body>
<script type="text/javascript">
<!--
function checkDates(date1, date2)
{
var fromMatch = /^(d{4})-(d{2})-(d{2})$/.exec(date1);
if (fromMatch == null) return false;
var toMatch = /^(d{4})-(d{2})-(d{2})$/.exec(date2);
if (toMatch == null) toMatch = fromMatch;
fromday = new Date(fromMatch[1], --fromMatch[2], fromMatch[3]);
today = new Date(toMatch[1], --toMatch[2], toMatch[3]);
timeLeft = (today.getTime() - fromday.getTime());
if(timeLeft <= 0) return false; else return true;
}
<i> </i>function toDays(date1, date2)
<i> </i>{
<i> </i> var fromMatch = /^(d{4})-(d{2})-(d{2})$/.exec(date1);
<i> </i> var toMatch = /^(d{4})-(d{2})-(d{2})$/.exec(date2);
<i> </i> fromday = new Date(fromMatch[1], --fromMatch[2], fromMatch[3]);
<i> </i> today = new Date(toMatch[1], --toMatch[2], toMatch[3]);
<i> </i> msPerDay = 24 * 60 * 60 * 1000 ;
<i> </i> timeLeft = (today.getTime() - fromday.getTime());
<i> </i> e_daysLeft = timeLeft / msPerDay;
<i> </i> daysLeft = Math.floor(e_daysLeft);
<i> </i> tdays = Math.floor(e_daysLeft);
<i> </i> return daysLeft;
<i> </i>}
<i> </i>Date.fromW3CDTF = function (s) {
<i> </i> return (/^(d{4})-(dd?)-(dd?)$/).test(s) ? new Date(RegExp.$1, RegExp.$2 - 1, RegExp.$3) : new Date (s)
<i> </i>}
<i> </i>Date.prototype.toW3CDTF = function () {
<i> </i> return [this.getFullYear(), this.getMonth() < 9 ? '0' + (this.getMonth() + 1) : this.getMonth() + 1, this.getDate() < 10 ? '0' + this.getDate() : this.getDate()].join ('-')
<i> </i>}
<i> </i>Date.prototype.incrementByDays = function (n) {
<i> </i> this.setDate (this.getDate () + parseInt(n, 10));
<i> </i> return this;
<i> </i>}
//-->
</script>
<form name="Szukaj_zasobow" method="post" action="">
<table border="0" cellpadding="0" cellspacing="0" width="232">
<tr>
<td style="padding: 5px;" align="center" bgcolor="#6ba6e8" valign="top" width="226"><table border="0" cellpadding="2" cellspacing="0" width="100%">
<tbody><tr>
<td class="txtb" align="right">from</td>
<td> <input name="od" id="od" size="10" maxlength="10" value="2005-01-01" type="text"> </td>
</tr> <tr>
<td class="txtb" align="right">to</td>
<td><input name="dod" id="dod" size="10" maxlength="10" value="" type="text">
</td>
</tr>
<tr>
<td class="txtb" align="right">days</td>
<td><input name="dni" id="dni" size="3" maxlength="3" value="" onchange="if(document.forms['Szukaj_zasobow'].od.value!='') { document.forms['Szukaj_zasobow'].dod.value = new Date.fromW3CDTF(document.forms['Szukaj_zasobow'].od.value).incrementByDays(document.forms['Szukaj_zasobow'].dni.value).toW3CDTF()}" type="text"></td>
</tr>
</tbody></table>
</td>
</tr>
</table>
</form>
</body></html>
[CODE]
Date.prototype.incrementByDays = function (n) {
this.setDate (parseInt(this.getDate()) + parseInt(n));
return this;
}
[/CODE]
you need it only around the n, and you should also specify to base 10.
copy the code from my post.
or at least use this line:
this.setDate (this.getDate () + parseInt(n, 10));[/QUOTE]
[i]From ECMA 262[/i]
parseInt (string , radix)
The parseInt function produces an integer value dictated by interpretation of the contents of the
string argument according to the specified radix. Leading whitespace in the string is ignored. If radix
is undefined or 0, it is assumed to be 10 except when the number begins with the character pairs 0x
or 0X, in which case a radix of 16 is assumed. Any radix-16 number may also optionally begin with
the character pairs 0x or 0X.
When the parseInt function is called, the following steps are taken:
1. Call ToString(string).
2. Let S be a newly created substring of Result(1) consisting of the first character that is not a
StrWhiteSpaceChar and all characters following that character. (In other words, remove leading
white space.)
3. Let sign be 1.
4. If S is not empty and the first character of S is a minus sign -, let sign be -1.
5. If S is not empty and the first character of S is a plus sign + or a minus sign -, then remove the
first character from S.
6. Let R = ToInt32(radix).
7. If R = 0, go to step 11.
8. If R < 2 or R > 36, then return NaN.
9. If R = 16, go to step 13.
10. Go to step 14.
11. Let R = 10.
12. If the length of S is at least 1 and the first character of S is “0”, then at the implementation's
discretion either let R = 8 or leave R unchanged.
13. If the length of S is at least 2 and the first two characters of S are either “0x” or “0X”, then
remove the first two characters from S and let R = 16.
14. If S contains any character that is not a radix-R digit, then let Z be the substring of S consisting of
all characters before the first such character; otherwise, let Z be S.
15. If Z is empty, return NaN.
16. Compute the mathematical integer value that is represented by Z in radix-R notation, using the
letters A-Z and a-z for digits with values 10 through 35. (However, if R is 10 and Z contains more
than 20 significant digits, every significant digit after the 20th may be replaced by a 0 digit, at
the option of the implementation; and if R is not 2, 4, 8, 10, 16, or 32, then Result(16) may be an
implementation-dependent approximation to the mathematical integer value that is represented by
Z in radix-R notation.)
17. Compute the number value for Result(16).
18. Return sign × Result(17).
Two things follow from this:0.1.9 — BETA 4.29