Javascript Reference

Show invariant(en-us?) datetime formate

@Feb. 23, 2010, 11:10 a.m.
Example of showing day of week and monthname using javascript:
Sunday, 17 May, 2009 13:10:44.9

Codes:
<div id="pltime0517">Sunday, 17 May, 2009 13:10:44.9</div>
<script type="text/javascript">
UpdateTime();
function UpdateTime() {
    var days = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
    var months = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
    var d = new Date();
    document.getElementById('pltime0517').innerHTML = days[d.getDay()] + ', ' + d.getDate() + ' ' + months[d.getMonth()] + ', ' + d.getFullYear() + ' ' + ('0' + d.getHours()).slice(-2) + ':' + ('0' + d.getMinutes()).slice(-2) + ':' + ('0' + d.getSeconds()).slice(-2) + '.' + ('' + d.getMilliseconds()).slice(0, 1);
    setTimeout('UpdateTime()', 100);
}
</script>

Note:
string.substr(-2) doesn't work out the same results on all browsers.

Hello ddy,
Thank you.
You are right. IE doesn't work the same as FF. Sorry, it's my fault. I will update them.
javascript manual - 2010-02-22
Ah well, in IE7 and previous it doesn't work that way anyway (IE8?) so if you want your code to work as expected for most people, you should use slice(-2). Incidentally, the documentation in this site for substr doesn't talk about negative indexes while the slice documentation does. I realize that the ecmascript final doc DOES talk about negative indexes for substr but apparently IE doesn't comply. To see what I mean, bring up IE and in the address bar type javascript:'abcdef'.substr(-2) and you'll get 'abcdef', not 'ef'. FF works as the current standard says. Sorry for the confusion.
ddy - 2010-02-22
No. -2 begins from the end.
'abcdefg'.substr(2) = 'cdefg'
'abcdefg'.substr(-2) = 'fg'
javascript manual - 2010-02-21

Powered by Linode.