0

I have this script to display the current date and time in my web page:

<script type = "text/javascript">

function startTime() {
var now = new Date();
var h=now.getHours();
var min=now.getMinutes();
var s=now.getSeconds();
var ampm=(now.getHours()>11)?"PM":"AM";
var d=now.getDay();
var y=now.getFullYear();
var mon=now.getMonth();
var da=now.getDate();
var endings=["st","nd","rd","th"];
var dayendings=[0,1,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,0,2,3,4,4,4,4,4,4,1];
var days=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
var months=["January","February","March","April", "May", "June","July","August","September", "October", "Novemeber","Decemeber"];
if (h>12) {h-=12}
if (h==0) {h=12};
if (min<10) {min="0"+min}
if (s<10) {s="0"+s}
da+=endings[dayendings[da]];
if (da<10) {da="0"+da};
d=days[d];
mon=months[mon-1];
document.getElementById("time").innerHTML=d+", "+mon+" "+da+", "+y+" "+h+":"+min+":"+s+" "+ampm;
var tim = setTimeout("startTime()",1000);
}

</script>

But the result is like this:

Friday, April NaN, 2012 6:37:36 PM

How can I make April NaN, 2012 right? It must be the date today which is May 25,2012

0

5 Answers 5

4

At the very least you should change

da+=endings[dayendings[da]];

to

da+=endings[dayendings[da - 1]];

because right now it has an off-by-one bug (da is 1 to 31, but the indexes on dayendings are 0 to 30).

This should immediately fix your problem if "today" is the 31st of the month.

Fixes are also needed for the values in dayendings, as 4 is not meaningful there.

On a side note, you really should ditch this code for something better. Wouldn't you prefer it like this?

function startTime() {
    document.getElementById("time").innerHTML =
        moment().format("dddd, MMMM Do, YYYY h:mm:ss A");
}
Sign up to request clarification or add additional context in comments.

Comments

2

Your var dayendings=[0,1,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,0,2,3,4,4,4,4,4,4,1]; includes some 4 values. Yet, var endings=["st","nd","rd","th"]; has only a length of 4, so that it will return undefined when you access endings[4].

Also, you may get an off-by-one-error because da is in the range 1 to 31, which are not the indizes of your dayendings array.

Comments

0

It appears that your logic for the counter is not correct. Try the following code:

function makeArray() {
for (i = 0; i<makeArray.arguments.length; i++)
this[i + 1] = makeArray.arguments[i];
}
var now = new Date();
var h=now.getHours();
var min=now.getMinutes();
var s=now.getSeconds();
var ampm=(now.getHours()>11)?"PM":"AM";
var months = new makeArray('January','February','March','April','May',
'June','July','August','September','October','November','December');
var date = new Date(); 
var day = date.getDate();
var month = date.getMonth() + 1;
var yy = date.getYear();
var year = (yy < 1000) ? yy + 1900 : yy;
var endings=["st","nd","rd","th"];
var dayendings=[0,1,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,0,2,3,4,4,4,4,4,4,1];
var days=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"];

document.write(days[date.getDay()] + " " + months[month]+ " " + day + ", " + year + " " + h + ":" + min + ":" + s + " " + ampm);

3 Comments

this works, but i need the time to be refreshed at every second
In that case just have the function call itself.Put the var tim=settimout(...) right after document.write... This link can give you an example. Hope this helps. w3schools.com/js/js_timing.asp
As a side note the below answers are cleaner, so once you get it working where it is updating as you need it you might want to re-factor the script.
0

I've a relatively simple DateTime object in this jsfiddle. May be useful? In your case, using this object, this command would show the date in the format you want (apart from the PM/AM time formatting)

XDate({date:new Date,lang:'EN'}).format('WD, MM dd, yyyy, hh:mm:se0');
//=> Friday, May 25 2012, 13:05:03 

Comments

0

first generate this function and save as .js file

function date_time(id) {
    date = new Date;
    year = date.getFullYear();
    month = date.getMonth();
    months = new Array('January', 'February', 'March', 'April', 'May', 'June', 'Jully', 'August', 'September', 'October', 'November', 'December');
    d = date.getDate();
    day = date.getDay();
    days = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
    h = date.getHours();
    if (h < 10) {
        h = "0" + h;
    }
    m = date.getMinutes();
    if (m < 10) {
        m = "0" + m;
    }
    s = date.getSeconds();
    if (s < 10) {
        s = "0" + s;
    }
    result = '' + days[day] + ' ' + months[month] + ' ' + d + ' ' + year + ' ' + h + ':' + m + ':' + s;
    document.getElementById(id).innerHTML = result;
    setTimeout('date_time("' + id + '");', '1000');
    return true;
}

then use this code on your html page to display the current time and date

<script type="text/javascript"> window.onload = date_time('date_time'); </script>

dont forget the script tag on the head part of your page: ex

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.