Advance Date +15 Days

G

Guest

I have a script...
-----
<SCRIPT language="JavaScript" type="text/javascript">
<!--
function makeArray() {
for (i = 0; i<makeArray.arguments.length; i++)
this[i + 1] = makeArray.arguments;
}

function makeArray0() {
for (i = 0; i<makeArray0.arguments.length; i++)
this = makeArray0.arguments;
}

function y2k(number) { return (number < 1000) ? number + 1900 : number; }

var months = new
makeArray('January','February','March','April','May','June','July','August','September','October','November','December');
var days = new
makeArray0('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');

var today = new Date();
var day = days[today.getDay()];
var date = today.getDate( ) + 15;
var month = today.getMonth() + 1;
var year = y2k(today.getYear());

document.write(months[month] +' ' + date + ', ' + year);
//-->
</SCRIPT>
---

I need this script to advance the document.write by 15 days, as you can see
I tried adding "+ 15"
after the Var Date = today.getDate( ) that works fin until it started
showing 32 days in June
today. LOL.

Can anyone please assist me? (e-mail address removed)

Thank you,
Robert
 
J

Jc

I need this script to advance the document.write by 15 days, as you can see
I tried adding "+ 15"
after the Var Date = today.getDate( ) that works fin until it started
showing 32 days in June
today.

The date constructor can take a number of milliseconds as an argument
to create a date object from. Perhaps this example will be of use:

<script>
var dt = new Date();
var dt2 = new Date(dt.getTime()+15*24*60*60*1000);
alert(dt+"\n"+dt2);
</script>

Refer to:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/js56jsobjdate.asp
 
M

Mick White

document.write(months[month] +' ' + date + ', ' + year);
//-->
</SCRIPT>
---

<script type="text/javascript">
var today = new Date();
today.setDate(today.getDate()+15);
document.write(
['January','February','March','April','May','June','July',
'August','September','October','November','December'][today.getMonth()]+
" "+today.getDate()+
", "+today.getFullYear()
);
</script>

if you find yourself using it a lot, create a Date.prototype

Date.prototype.addDays=function(days){
this.setDate(this.getDate()+days)
// do stuff with the revised date object if you want to.
}

Mick

Mick
 
R

RobG

I have a script...

The language attribute is depreciated:


Hiding scripts with HTML comments is unnecessary and potentially
harmful, just don't do it.
function makeArray() {
for (i = 0; i<makeArray.arguments.length; i++)
this[i + 1] = makeArray.arguments;
}

function makeArray0() {
for (i = 0; i<makeArray0.arguments.length; i++)
this = makeArray0.arguments;
}


Neither of these functions is needed, see below.
function y2k(number) { return (number < 1000) ? number + 1900 : number; }

var months = new
makeArray('January','February','March','April','May','June','July','August','September','October','November','December');

var months = [
'January','February','March','April','May','June',
'July','August','September','October','November','December'
];
var days = new
makeArray0('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');

var days = [
'Sunday','Monday','Tuesday','Wednesday',
'Thursday','Friday','Saturday'
];
var today = new Date();

Relying on the client PC's clock is unreliable, there are other
strategies, read the group FAQ:

<URL:http://www.jibbering.com/faq>

You can add 15 days directly to the date object:

var today = new Date();
today.setDate(today.getDate()+ 15);
var day = days[today.getDay()];
var date = today.getDate( ) + 15;
var month = today.getMonth() + 1;
var year = y2k(today.getYear());

Delete all the above.
document.write(months[month] +' ' + date + ', ' + year);

document.write(
months[today.getMonth()] + ' '
+ days[today.getDay()] + ' '
+ today.getFullYear()
);
</SCRIPT>
---

I need this script to advance the document.write by 15 days, as you can see
I tried adding "+ 15"
after the Var Date = today.getDate( ) that works fin until it started
showing 32 days in June
today. LOL.

Sorry, but the laugh's on you. The value returned by getDate() is a
number, so adding any other number will give a normal arithmetic
addition.


<script type="text/javascript">

var months = [
'January','February','March','April','May','June',
'July','August','September','October','November','December'
];

var days = [
'Sunday','Monday','Tuesday','Wednesday',
'Thursday','Friday','Saturday'
];

var today = new Date();
today.setDate(today.getDate()+ 15);

document.write(
days[today.getDay()] + ', '
+ today.getDate() + ' '
+ months[today.getMonth()] + ', '
+ today.getFullYear()
);
</script>
 
D

Dr John Stockton

JRS: In article <[email protected]
m>, dated Fri, 17 Jun 2005 15:32:11, seen in (e-mail address removed) posted :
I have a script...

Delete it, learn javascript, read and understand the newsgroup FAQ, and
start again.

<SCRIPT language="JavaScript" type="text/javascript">
^^^^^^^^^^^^^^^^^^^^^ not required
<!--
function makeArray() {
for (i = 0; i<makeArray.arguments.length; i++)
this[i + 1] = makeArray.arguments;
}

function makeArray0() {
for (i = 0; i<makeArray0.arguments.length; i++)
this = makeArray0.arguments;
}


Not needed. And if you want the first word to be indexed 0 or 1, use a
parameter rather than two routines, or supply a dummy zeroth entry.

function y2k(number) { return (number < 1000) ? number + 1900 : number; }

Not correct in all systems; see FAQ & sig. Function getFullYear is
generally available; and, if not, can be accurately emulated.

However, since your work is unlikely to be wanted for very long, you can
safely use 2000 + number%100 with a note about its range.

var months = new
makeArray('January','February','March','April','May','June','July','August','Se
p
tember','October','November','December');

DO NOT let your posting agent break lines of code; do it yourself.
Posted code should be directly executable.

var months = ['January', 'February', 'March', 'April', 'May',' June',
'July', 'August', 'September', 'October', 'November', 'December']
var days = new
makeArray0('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturda
y
');

var days = ['Sunday', 'Monday', 'Tuesday',
'Wednesday', 'Thursday', 'Friday', 'Saturday']


var today = new Date();
var day = days[today.getDay()];
var date = today.getDate( ) + 15;
var month = today.getMonth() + 1;
var year = y2k(today.getYear());

document.write(months[month] +' ' + date + ', ' + year);

Don't use FFF, or anything like it, on the WWW.

I need this script to advance the document.write by 15 days, as you can see
I tried adding "+ 15"
after the Var Date = today.getDate( ) that works fin until it started
showing 32 days in June
today.

So you were, on the previous day, prepared to believe June 31st?

Yes, that's exactly what you asked for, and should have foreseen.

You need to increment the date object, not the day-of-month.

Ignore any response containing 864e5 or its equivalent; it will not be
reliable for all potential users. Consider


function LZ(x) { return (x<0||x>=10?"":"0") + x }

with (new Date()) {
setDate(getDate()+15)
document.writeln(
getFullYear(), '-', LZ(getMonth()+1), '-', LZ(getDate()) ) }
 
D

Dr John Stockton

JRS: In article <42b37997$0$11333$5a62ac22@per-qv1-newsreader-
01.iinet.net.au>, dated Sat, 18 Jun 2005 11:32:02, seen in
news:comp.lang.javascript said:
Relying on the client PC's clock is unreliable, there are other
strategies, read the group FAQ:

<URL:http://www.jibbering.com/faq>

Which section? 4.30 refers to the page server clock.

My view is that if the date/time matters to the page author's
organisation, then it can be applied to the submitted response after
receipt at the server end; but if otherwise then it's up to the end user
to have his clock set as he wishes.

<FAQENTRY> FAQ 4.17 is in error in the text; the current date is not
unique. The code rightly uses the current GMT/UTC date/time, which is
(near enough) unique (the clock may be corrected backwards [but no
concern for the end of Summer Time]). Also, an "of" should be an "or".

FAQ 4.31, "everytime" -> "every time"
 
R

RobG

Dr said:
JRS: In article <42b37997$0$11333$5a62ac22@per-qv1-newsreader-
01.iinet.net.au>, dated Sat, 18 Jun 2005 11:32:02, seen in



Which section? 4.30 refers to the page server clock.

Section 3.2 has "Manipulating times, dates and the lastModified date
and time in javascript:-" followed by a link to your pages, where
there is a section "Some date troubles".

I was highlighting that using the client PC solely to determine the
date or time is not sensible. If the intention is to advise an
approximate delivery time or similar, it may be best to use dates and
times based on some accurate datum convenient to the OP, rather than
the client, then suggest what the equivalent *might* be at the client
location based on the client system date/time and tell the user that
that is how they were calculated.
 
D

Daniel Kirsch

I need this script to advance the document.write by 15 days,

Maybe the following script might be of use for you:

var fst = "+15d";
var dte = new Date();
var plusMinus = fst.match(/[+-]\d+[smhdy]/gi);
if (plusMinus) {
var vorz, shrt, val;
var multiplier = {"S":1,"M":60,"H":3600,"d":86400};
for (var i=0; i<plusMinus.length; i++) {
vorz = plusMinus.charAt(0) == "+" ? 1 : -1;
shrt = plusMinus.charAt(plusMinus.length-1);
val = vorz*Number(plusMinus.substring(1,plusMinus.length-1));
if (shrt == "m")
dte.setMonth(dte.getMonth() + val);
else if (shrt.toLowerCase() == "y")
dte.setFullYear(dte.getFullYear() + val);
else if (multiplier[shrt])
dte.setTime(dte.getTime() + (val*multiplier[shrt]*1000));
}
}
alert(dte);

You may now add or substract seconds, minutes, hours, days, months or
years to your Date object (dte).

Use "+5S" for fst to add 5 seconds, use "-10d" to substract 10 days, use
"-2H" to substract 2 hours etc.
S = Seconds
M = Minutes
H = Hours
d = Days
m = Months
y = Years

Daniel
 
C

Christopher Benson-Manica

RobG said:
Hiding scripts with HTML comments is unnecessary and potentially
harmful, just don't do it.

Why so? It seems to be a rather ubiquitous idiom, and in any case how
else would one hide script from user agents that can't or won't
execute it?
 
M

Michael Winter


Potentially harmful? Well for one thing, if the practice continues into
XHTML markup, then authors will be rather shocked to find that their
script really is hidden from browsers - all of them.
It seems to be a rather ubiquitous idiom,

Tag soup is just as pervasive. What's your point?
and in any case how else would one hide script from user agents that
can't or won't execute it?

One wouldn't.

The original point of 'hiding' was to make sure that an old browser
(that is, old at the end of the last century) wouldn't show the contents
of a SCRIPT element. All user agents now in use understand what a SCRIPT
element is, whether they are capable of executing them or not, so this
is entirely unnecessary. The fact that it persists in modern markup
shows just how clueless some people are.

Mike
 
C

Christopher Benson-Manica

Michael Winter said:
Potentially harmful? Well for one thing, if the practice continues into
XHTML markup, then authors will be rather shocked to find that their
script really is hidden from browsers - all of them.

We're a long way from XHTML, despite my desires otherwise...
The original point of 'hiding' was to make sure that an old browser
(that is, old at the end of the last century) wouldn't show the contents
of a SCRIPT element. All user agents now in use understand what a SCRIPT
element is, whether they are capable of executing them or not, so this
is entirely unnecessary. The fact that it persists in modern markup
shows just how clueless some people are.

Well, I appreciate the clue :)
 
M

Michael Winter

Michael Winter said:
The fact that [script hiding] persists in modern markup shows just
how clueless some people are.

Well, I appreciate the clue :)

No offense. ;)

Script 'hiding' has been around for so long that many don't know why it
began in the first place, or that it stopped being necessary years ago.

Mike
 
T

T. Postel

The original point of 'hiding' was to make sure that an old browser
(that is, old at the end of the last century) wouldn't show the contents
of a SCRIPT element. All user agents now in use understand what a SCRIPT
element is, whether they are capable of executing them or not, so this
is entirely unnecessary. The fact that it persists in modern markup
shows just how clueless some people are.

Mike

However, several search engines do pay attention to HTML comments and don't
index keywords found inside them. Much modern usage of comment tags around
<script> and <style> code is to prevent search engines from displaying pages
which may contain keywords in the code.
Perhaps there is more than one clue?
 
M

Michael Winter

On 20/06/2005 18:07, T. Postel wrote:

[snip]
However, several search engines do pay attention to HTML comments and don't
index keywords found inside them.

If you don't want to index a script, then don't put it in a position to
be indexed. In the vast majority of cases, scripts should be served from
a separate file, and not embedded.

[snip]

Mike
 
C

Christopher Benson-Manica

Michael Winter said:
Script 'hiding' has been around for so long that many don't know why it
began in the first place, or that it stopped being necessary years ago.

Does this truly go for all modern browsers, including PDA-based
browsers? I believe we had a situation where some PDA browser
actually crashed on <script> tags, although that wouldn't have been
fixed by script hiding.
 
M

Michael Winter

Michael Winter said:
Script 'hiding' [...] stopped being necessary years ago.

Does this truly go for all modern browsers, including PDA-based
browsers?

There's absolutely no reason why it shouldn't, but I didn't really mean
to say 'all'. No-one could make that claim.

All a user agent needs to do once it reads a SCRIPT opening tag is
ignore everything until it encounters </ (though </script is probably
safer). Even something that's strapped for resources should have no
problem doing this as it needs to for SGML comments.

The SCRIPT (and STYLE) elements were defined in 1996. If a user agent
written after HTML 3.2 doesn't understand them, then it's broken. But,
as I said elsewhere, if you have a genuine concern, then a better
solution would be to move the script to an external file. Non-trivial
scripts should be separated, anyway.

[snip]

Mike
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Mon, 20 Jun
2005 13:24:49, seen in Daniel Kirsch
I need this script to advance the document.write by 15 days,

Maybe the following script might be of use for you:

var fst = "+15d";
var dte = new Date();
var plusMinus = fst.match(/[+-]\d+[smhdy]/gi);
if (plusMinus) {
var vorz, shrt, val;
var multiplier = {"S":1,"M":60,"H":3600,"d":86400};
for (var i=0; i<plusMinus.length; i++) {
vorz = plusMinus.charAt(0) == "+" ? 1 : -1;
shrt = plusMinus.charAt(plusMinus.length-1);
val = vorz*Number(plusMinus.substring(1,plusMinus.length-1));
if (shrt == "m")
dte.setMonth(dte.getMonth() + val);
else if (shrt.toLowerCase() == "y")
dte.setFullYear(dte.getFullYear() + val);
else if (multiplier[shrt])
dte.setTime(dte.getTime() + (val*multiplier[shrt]*1000));
}
}
alert(dte);




Not good.

If new Date('2005/10/21'); is used, then, since there are 31 days in
October, the answer should be November 5th. However, if executed in the
EU or in much of North America, the result will be in November 4th.

Did you not see that I warned the OP "Ignore any response containing
864e5 or its equivalent;"?

See below.
 
R

Richard Cornford

T. Postel said:
However, several search engines do pay attention to HTML
comments and don't index keywords found inside them.

Not indexing the contents of comments in HTML seems like a perfectly
reasonable thing for a search engine to do. They are unlikely to contain
anything of relevance to someone doing any sort of search.
Much modern usage of comment tags around <script> and
<style> code is to prevent search engines from displaying
pages which may contain keywords in the code.

Is this true? Strictly speaking what appear to be HTML comments within
SCRIPT and STYLE elements are not comments at all because the contents
of those elements are CDATA so the HTML comment-like delimiters are no
more than sequences of charters that resemble HTML comments. They only
get to be HTML comments when the parser has no understanding of SCRIPT
and STYLE elements.

Now SCRIPT and STYLE elements have been with us for some considerable
time, and browsers at least have understood how to handle them (either
using them or ignoring their contents) for almost as long.

So how does the writer of an HTML parser for a search engine come to be
unfamiliar with the nature of SCIPT and STYLE elements? And how does the
resulting search engine perform when it is diluting its usefulness
considering identifiers, script comments and CSS class names.

This sounds more like someone inventing a fairly tail to justify doing
something that they could not find any other reason for doing. An
nothing short of search engine documentation stating that they index
SCRIPT and STYLE element content will convince me otherwise.
Perhaps there is more than one clue?

Or a second type of clueless.

Richard.
 
D

Daniel Kirsch

Dr said:
Not good.

If new Date('2005/10/21'); is used, then, since there are 31 days in
October, the answer should be November 5th. However, if executed in the
EU or in much of North America, the result will be in November 4th.

Did you not see that I warned the OP "Ignore any response containing
864e5 or its equivalent;"?

Thanks. Added
if (shrt == "d")
dte.setDate(dte.getDate() + val);

Anything else "not good"?

Daniel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top