我是JavaScript的新手(但不是编程),我很难搞清楚我在这个函数中犯了哪个错误,在这里找到:http://mikeryan.webatu.com/function.html [dead link] [猜测原文底部的代码]
该函数应采用 DDMMYY 时间戳并将其转换为人类可读的字符串。例如, 210710
会变成 July 21st, 2010
。
更新:代码可能与OP的死链接相似:
function timestamp(d){
var year = (d-(Math.round(d / 100)*100);
var day = Math.floor(d/10000);
var dayfix = (day - (Math.floor(day/10)*10));
// var month = ((d-year)-(day*100000)/100);
var a = (d - year);
var b = ((day * 100000) / 10);
var month = (a - b) / 100;
var months = new Array();
months[1] = "January";
months[2] = "February";
months[3] = "March";
months[4] = "April";
months[5] = "May";
months[6] = "June";
months[7] = "July";
months[8] = "August";
months[9] = "September";
months[10] = "October";
months[11] = "November";
months[12] = "December";
var daysuffix = new Array();
daysuffix[0] = "th";
daysuffix[1] = "st";
daysuffix[2] = "nd";
daysuffix[3] = "rd";
daysuffix[4] = "th";
daysuffix[5] = "th";
daysuffix[6] = "th";
daysuffix[7] = "th";
daysuffix[8] = "th";
daysuffix[9] = "th";
if(year>20){
year = '19' + year;
}
else{
year = '20' + year;
}
return (months[month] + ' ' + day + daysuffix[dayfix] + ', ' + year);
}
答案 0 :(得分:5)
一个问题:你错过了一个括号。变化:
var year = (d-(Math.round(d / 100)*100);
到
var year = (d-(Math.round(d / 100)*100));
话虽如此,这是一种更直接的计算方法:
var year = d % 100;
var month = Math.floor(d / 100) % 100;
var day = Math.floor(d / 10000) % 100;
接下来,您的数组初始化不必要地冗长。而不是:
var arr = new Array();
arr[0] = "foo";
arr[1] = "bar";
只是这样做:
var arr = ["foo", "bar"];
您的日期后缀不正确。它将“nd”置于12和“4月12日”之后显然不正确。我只是使用逻辑来做这个而不是查找数组,其中大多数元素是“th”。
所以:
function timestamp(d){
var year = d % 100;
var month = Math.floor(d / 100) % 100;
var day = Math.floor(d / 10000) % 100;
var months = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"];
if (year>20) {
year = '19' + year;
} else {
year = '20' + year;
}
if (day == 1 || day == 21 || day == 31) {
var suffix = "st";
} else if (day == 2 || day == 22) {
var suffix = "nd";
} else {
var suffix = "th";
}
return (months[month-1] + ' ' + day + suffix + ', ' + year);
}
最后,您的“timestamp”在其当前形式中是一个整数,几乎没有价值。 YYYYMMDD更典型的格式有两种:
数字排序符合日期排序;以及
这是毫不含糊的。北美人前一个月(即MMDDYY)。世界上的其他人都把第一天放在第一位(即DDMMYY)。没有人做YYDDMM。
答案 1 :(得分:0)
我使用%
- modulo:X modulo 100丢弃除最后2位数之外的任何内容。有用!
也使用floor
而不是round
答案 2 :(得分:0)
使用日期对象。它的速度要快得多。这是一个快速的例子,它假设年份将在2000年代,所以你必须做一些修改。输出并不完全是你所拥有的,但它非常接近,而且代码要短得多。
function date(e){
var d = new Date();
d.setYear(2000+e.substring(4)/1,e.substring(2,4)-1,e.substring(0,2)-1);
alert(d.toDateString());
}
答案 3 :(得分:0)
首先使用Math.floor获取小数的最低值,然后在你的函数中有一些拼写错误。这是可行的代码(注意:只是通过几个例子进行了测试),但应该足以让你开始:
function timestamp(d){
var year = (d-(Math.floor(d / 100)*100));
var day = Math.floor(d/10000);
var dayfix = (day - (Math.floor(day/10)*10));
// var month = ((d-year)-(day*100000)/100);
var a = (d - year);
var b = ((day * 100000) / 10);
var month = (a - b) / 100;
var months = new Array();
months[1] = "January";
months[2] = "February";
months[3] = "March";
months[4] = "April";
months[5] = "May";
months[6] = "June";
months[7] = "July";
months[8] = "August";
months[9] = "September";
months[10] = "October";
months[11] = "November";
months[12] = "December";
var daysuffix = new Array();
daysuffix[0] = "th";
daysuffix[1] = "st";
daysuffix[2] = "nd";
daysuffix[3] = "rd";
daysuffix[4] = "th";
daysuffix[5] = "th";
daysuffix[6] = "th";
daysuffix[7] = "th";
daysuffix[8] = "th";
daysuffix[9] = "th";
if(year>20){
year = '19' + year;
}
else{
year = '20' + year;
}
return (months[month] + ' ' + day + daysuffix[dayfix] + ', ' + year);
}