admin管理员组

文章数量:1397065

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

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

Share Improve this question edited May 25, 2012 at 10:48 Andrzej Doyle 104k33 gold badges191 silver badges231 bronze badges asked May 25, 2012 at 10:41 Alai AngeloAlai Angelo 611 gold badge3 silver badges6 bronze badges 0
Add a ment  | 

5 Answers 5

Reset to default 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");
}

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.

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);

I've a relatively simple DateTime object in this jsfiddle. May be useful? In your case, using this object, this mand 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 

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

本文标签: Show current date and time in javascriptStack Overflow