/    Sign up×
Community /Pin to ProfileBookmark

faulty syntax?

? I was looking for time/date display script that could call the code from a external .js file.

I found this, but can’t get it to work.
I don’t write java but am an experienced web developer and know CSS.
There are already obvious mistakes in the instructions.
Can anybody see any syntax arrors in this script?

Website URL: [url]http://javascriptsource.com/time-date/basic-date-display-2.html[/url]

Description: This script will display the month, day, and year, without the day of the week. Clean and simple. The display can be changed with CSS.

Follow the instructions below to use this script on your Web site.

<!– Paste this code into an external JavaScript file named: dateDisplay.js.js –>

/* This script and many more are available free online at
The JavaScript Source :: [url]http://javascript.internet.com[/url]
Created by: Lee Underwood :: [url]http://javascript.internet.com/[/url] *
/

function displayDate() {
var now = new Date();
var today = now.getDate();
var month = now.getMonth();
var monthName = new Array(12)
monthName[0]=”January “;
monthName[1]=”February “;
monthName[2]=”March “;
monthName[3]=”April “;
monthName[4]=”May “;
monthName[5]=”June “;
monthName[6]=”July “;
monthName[7]=”August “;
monthName[8]=”September “;
monthName[9]=”October “;
monthName[10]=”November “;
monthName[11]=”December “;
var year = now.getFullYear();

document.write(monthName[month]+today+ “, “+year);
}

<!– Paste this code into the BODY section of your HTML document
where you want the date displayed –>

<script type=”text/javascript”>
<!–
displayDate();
//–>
</script>

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@vwphillipsApr 06.2008 — <!-- Paste this code into an external JavaScript file named: dateDisplay.js.js -->[/QUOTE]
should read

<!-- Paste this code into an external JavaScript file named: dateDisplay.js -->[/QUOTE]

then within the page BODY tags


[CODE]<script src="dateDisplay.js" type="text/javascript"> [/CODE]

assuming it is the same directory as your page
Copy linkTweet thisAlerts:
@virt8574authorApr 06.2008 — Yes that was part of the problem.

But there is still a syntax problem somewhere in the code.

I went to:

http://www.javascriptlint.com/online_lint.php

to check it

Results

1 function displayDate() {

2 var now = new Date();

3 var today = now.getDate();

4 var month = now.getMonth();

5 var monthName = new Array(12)

6 monthName[0]="January ";

[COLOR="Red"]======^

lint warning: missing semicolon[/COLOR]


7 monthName[1]="February ";

8 monthName[2]="March ";

9 monthName[3]="April ";

10 monthName[4]="May ";

11 monthName[5]="June ";

12 monthName[6]="July ";

13 monthName[7]="August ";

14 monthName[8]="September ";

15 monthName[9]="October ";

16 monthName[10]="November ";

17 monthName[11]="December ";

18 var year = now.getFullYear();

19


20 document.write(monthName[month]+today+ ", "+year);

21 }

22

22

I don't see a missing semi-colon where specified.

I see one possibly missing from new Array(12)-Line 5
Copy linkTweet thisAlerts:
@Declan1991Apr 06.2008 — That was the missing one. Any error checker will not notice until the next line if there is a problem. And semi-colons aren't compulsory, which is why it is not technically a syntax error.
Copy linkTweet thisAlerts:
@virt8574authorApr 06.2008 — Thanks.

Crap... Still doesn't work.

Got everything in the same folder.

Think I'm following basic rules. It's not like it's the first time I've done this.
Copy linkTweet thisAlerts:
@Declan1991Apr 06.2008 — Try:[code=php]
var monthName = ["January ","February ","March ","April ","May ","June ","July ","August ","September ","October ","November ","December "];
[/code]
for <i>
</i>monthName[0]="January ";
monthName[1]="February ";
monthName[2]="March ";
monthName[3]="April ";
monthName[4]="May ";
monthName[5]="June ";
monthName[6]="July ";
monthName[7]="August ";
monthName[8]="September ";
monthName[9]="October ";
monthName[10]="November ";
monthName[11]="December ";
Copy linkTweet thisAlerts:
@virt8574authorApr 06.2008 — I finally had to use different script, stripped the script tags out and used 2 commands to get it to work, but I don't know why.

<!-- Begin

// Get today's current date.

var now = new Date();

// Array list of days.

var days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');

// Array list of months.

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

// Calculate the number of the current day in the week.

var date = ((now.getDate()<10) ? "0" : "")+ now.getDate();

// Calculate four digit year.

function fourdigits(number) {

return (number < 1000) ? number + 1900 : number;

}

// Join it all together

today = days[now.getDay()] + ", " +

months[now.getMonth()] + " " +

date + ", " +

(fourdigits(now.getYear())) ;

// Print out the data.

document.write(today);

// End -->

link in source code:

<script src="dateDisplay.js" type="text/javascript">

<script language="javascript" src="dateDisplay.js"></script>

Of course the whole idea here is to keep my source code clean.

I just don't know why this time, was a hassle.

I never got the original code I submitted in this thread to work.
×

Success!

Help @virt8574 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.2,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...