I'm trying to make a date to appear as Fri, Feb 17 2012
instead of Friday, February 17, 2012
Here is my code:
var n = new Date(),
ampm = 'am',
h = n.getHours(),
m = n.getMinutes(),
s = n.getSeconds();
if (h >= 12) {
if (h > 12) h -= 12;
ampm = 'pm'
}
if (h < 10) h = '0' + h;
if (m < 10) m = '0' + m;
t = n.toLocaleDateString();
var d =
t.split(',')[0].substring(0, 3)
+ ', '
+ t.split(',')[1].substring(1, 4)
+ ' '
+ t.split(',')[1].split(' ')[2]
+ t.split(',')[2]
+ ', '
+ ' '
+ h + ':' + m + ' ' + ampm;
var e = Math.round((new Date() - new Date('2012/01/01').getTime()) / 1000);
var f = [1];
var a = [];
var i = 0;
alert(d);
Now this works on some browsers but it doesnt on others. Some browsers show 17, 201, undefinedundefined, and some browsers show what I am looking for.
Can anyone spot what I am missing?
Fiddle: http://jsfiddle.net/nxyum/
Works on:
Windows: Chrome, Safari
Mac: Chrome, Opera
Doesn't work:
Windows: Firefox, Opera, IE
Mac: Firefox, Safari
I don't see what you are doing with your code...It's easy to output a date formatted any way you like-
Date.prototype.shortString= function(){
var D= this,
shortdays= ['Sun', 'Mon', 'Tues', 'Wed', 'Thur', 'Fri', 'Sat'],
shortmonths= ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul',
'Aug', 'Sept', 'Oct', 'Nov', 'Dec'];
return [shortdays[D.getDay()], ', ', shortmonths[D.getMonth()], ' ',
D.getDate(), ' ', D.getFullYear()].join('');
}
new Date().shortString()
/* returned value: (String)
Fri, Feb 17 2012
*/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.