2010-07-21 111 views
2

我是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); 
} 
+0

爲什麼不轉210710成字符串,然後做parseNumber上子讓你的價值觀?可能效率較低,但似乎更容易思考。 – 2010-07-21 03:36:27

+0

你看過JS的[Date Object](http://www.w3schools.com/jsref/jsref_obj_date.asp)嗎? – NullUserException 2010-07-21 03:41:07

+0

你接受了一個答案,但我提供了一個使用Date對象的方法,它檢出它,並檢查NullUserException所具有的鏈接。使用日期對象可能會更容易,然後嘗試自己解析數據。 – qw3n 2010-07-21 17:59:37

回答

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"]; 

你一天後綴不正確。它在12和12月4日後顯示「nd」顯然不正確。我只是用邏輯來做這件事,而不是使用大多數元素都是「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); 
} 

最後是在你的「時間戳」是目前形式的整數價值不大。這種類型的東西更典型的格式是YYYYMMDD,原因有兩個:

  1. 數字排序匹配日期排序;和

  2. 這是毫不含糊的。北美人前一個​​月(即MMDDYY)。世界上其他人都首先把日子放在首位(即DDMMYY)。沒有人會做YYDDMM。

+0

是的,我很累,完全錯過了那一個。非常感謝! – 2010-07-21 03:40:01

+0

謝謝,cletus!你編輯的答覆是非常有用的,對我來說是一個很好的學習經歷。 – 2010-07-21 03:55:56

+0

我首先選擇整數的唯一原因是在後面的函數中進行時間比較,以確定兩個日期之間的時間量。至於第二點,我意識到這個錯誤,並已改變它在服務器上。再一次感謝你的幫助! – 2010-07-21 04:04:17

0

我想用% - modulo:X modulo 100丟棄除最後2位數以外的任何東西。有用!

也使用floorround

+0

好的,謝謝你的提示。將進行更改。 – 2010-07-21 03:40:29

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