/    Sign up×
Community /Pin to ProfileBookmark

copying and pasting codes

? i was looking through javascript.com and found a cool code. I am new to using javascript.com and was wondering if someone could help me…

i copied and pasted a Page up for to my site and i can’t get the numbers to show. could someone look at this code and tell me where i am suppose to add the date for when I created my site?

thanks..

<html>
<HEAD>

<SCRIPT LANGUAGE=”JavaScript”>

<!– This script and many more are available free online at –>
<!– The JavaScript Source!! [url]http://javascript.internet.com[/url] –>

<!– Begin
function HowLongSince(startmonth, startdate, startyear) {
sdate=startdate;
smonth=startmonth-1;
syear=startyear;
var DaysInMonth = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
today = new Date()
var thisyear = today.getFullYear();
var thismonth = today.getMonth();
var thisdate = today.getDate();
mstart = new Date(syear,(smonth==12?1:smonth+1),1);
days1 = (mstart – new Date(syear,smonth,sdate))/(24*60*60*1000)-1;
mend = new Date(thisyear,thismonth,1);
days2 = (new Date(thisyear,thismonth,thisdate) – mend)/(24*
60*60*1000)+1;
dayst = days1 + days2;
if (dayst >= DaysInMonth[smonth]) {
AddOneMonth = 1;
dayst -= DaysInMonth[smonth];
}
else AddOneMonth = 0;
ydiff1 = thisyear-mstart.getFullYear();
mdiff1 = thismonth-mstart.getMonth()+AddOneMonth;
if (mdiff1 >11) { mdiff1=0; ydiff1++; }
if (mdiff1 < 0) { mdiff1 = mdiff1 + 12; ydiff1–; }
temp = (ydiff1==0?””?ydiff1==1?ydiff1+” year and “:ydiff1 + ” years, “));
temp += (mdiff1==0?”0 months, and “?mdiff1==1?mdiff1+” month, and “:mdiff1+” months, and “));
temp += (dayst==0?”no days”?dayst==1 ? ” 1 day.” : dayst + ” days.” ));
return temp;
}
// End –>
</script>
</HEAD>

<BODY>

<SCRIPT LANGUAGE=”JavaScript”>
<!– Begin
document.write(“The JavaScript Source has been up for “);
document.write(“<font color=red>”);
document.write(HowLongSince(06,15,97));
// this is the date your site was created. day,month,year
document.write(“</font>”);
// End –>
</script>

<p><center>
<font face=”arial, helvetica” size=”-2″>Free JavaScripts provided<br>
by <a href=”http://javascriptsource.com”>The JavaScript Source</a></font>
</center><p>

<!– Script Size: 1.96 KB –>
</body>
</html>

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@herodote92Aug 15.2005 — This works. Where you should mention your date is indicated :
<i>
</i>document.write(HowLongSince(06,15,97));
// this is the date your site was created. day,month,year


Only be careful, the HTML page is is created using "document.write". If you use "document.write", than the entire page should be created this way. Here it works because there is nothing else in the page than the site creation date. If you mix "normal" HTML and "document.write", this will destroy the plain HTML, because the page is re-generated.

To make it work another way, you should add an onload function to the body, like this:

<body onload="ShowHowLong();">

and in the head section, you add the function, something like:

function ShowHowLong() {

var my_mention = HowLongSince(06,15,97); // mm, dd, yy

document.getElementById("id_mention").firstChild.data = my_mention;

}

supposing that somewhere in the body section, you have an element like this, for ex:
<i>
</i>&lt;span id = "id_mention"&gt;&amp;nbsp;&lt;/span&gt;


So once the body is loaded, the "&nbsp;" will be replaced by the text you want.
Copy linkTweet thisAlerts:
@emrald_dragonauthorAug 15.2005 — okay, i would be mixing a document.write with a web building program which is html

not to sound stupid.. i'm fairly new at this..... the coding above is a counter that starts when i add it and keeps counting for how long my site has been up. after the code is up i don't have to alter it, it just runs.

the body on load is there a </body onload>? or is that still considered </body>?

and the section you said to put in the head section do i put before the other coding that goes in the head section?

oh, do i do < > for that section you mention to add?

and what does "&NBSP mean?
Copy linkTweet thisAlerts:
@emrald_dragonauthorAug 16.2005 — does some one know what i am asking Herodote92?
Copy linkTweet thisAlerts:
@herodote92Aug 16.2005 — Hmm, sorry that my English is not too good, but:

The date you started your site is a constant, you write it yourself once for all in the script. What the script does is simply comparing that constant with the computer's date, and showing the difference in a readable form. Of course, if the constant is false, or if the computer's date is false, the result will be false too. Be careful that months in Javascript start from 0, and not from 1, thus: January=0, February=1... December=11. But days and years should have their "normal" value.

There is no such thing as a </body onload>. "unload" is simply a facultative property of the body tag, just like "bgcolor" (background color) or others. So the syntax is:
<i>
</i>&lt;html&gt;
&lt;head&gt; ... &lt;/head&gt;
&lt;body onload="MyFunction();"&gt; ... &lt;/body&gt;
&lt;/html&gt;


In the head section, you can present the JS functions in any order, it's up to you. And even so if the functions call each other. It's a function I suggested to add, not a section.

"&nbsp;" is a standard HTML character code meaning something like "effective space", meaning that the browser will keep it in the result (if you write "A B" with many spaces in between, the browser will only show one on the screen, but if you write "A&nbsp;&nbsp;&nbsp;B", it will show 3 blanks between A and ?. I usually do so just in order to be sure that the <span> element would have a value when initializing.

Hope this all is understandable.
×

Success!

Help @emrald_dragon 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,
)...