我是JavaScript新手(但不是編程),我很難弄清楚我在這個函數中犯了什麼錯誤,在這裏找到:http://mikeryan.webatu.com/function.html[死鏈接] [底部原始代碼的猜測]將DDMMYY號碼轉換爲日期嗎?
該函數應採用DDMMYY時間戳,並將其轉換爲人類可讀字符串。例如,210710
將變成July 21st, 2010
。
UPDATE:代碼這可能是類似的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);
}
爲什麼不轉210710成字符串,然後做parseNumber上子讓你的價值觀?可能效率較低,但似乎更容易思考。 – 2010-07-21 03:36:27
你看過JS的[Date Object](http://www.w3schools.com/jsref/jsref_obj_date.asp)嗎? – NullUserException 2010-07-21 03:41:07
你接受了一個答案,但我提供了一個使用Date對象的方法,它檢出它,並檢查NullUserException所具有的鏈接。使用日期對象可能會更容易,然後嘗試自己解析數據。 – qw3n 2010-07-21 17:59:37