0

我使用 javascript 创建了一个表,其中包含 和 之类的ID,namebirth date。我还对该表进行了排序。

但是现在我必须将日期从dd mon yyyyto分开dd mon

例如:从4 Nov 19894 Nov

我应该怎么办?

这是代码......我只是想更新它......

<script>

    var now = new Date();

now.format("dd mmm");
var allNums = false;
var allDates = false;
var lastSort = -1;
var absOrder = true;

function setDataType(inValue) {


    var isDate = new Date(inValue);
    if (isDate == "NaN") {
        if (isNaN(inValue)){

            inValue =  inValue.toUpperCase();
            allNums = false
            allDates = false
            return inValue;
          }
        else {

            allDates = false
            return parseFloat(1*inValue);
          }
        }
  else {

        allNums = false
        return inValue ;
      }
  }

function sortTable(col){
    if (lastSort == col){

        absOrder ? absOrder = false : absOrder = true
    }
    else{
        absOrder = true
    }
    lastSort = col
    allTR = document.getElementById("dataTable").childNodes[0].childNodes

    totalRows = allTR.length
    colToSort = new Array()
    colArr = new Array()
    copyArr = new Array()
    resultArr = new Array()

    allNums = true
    allDates = true


    for (x=1; x < totalRows; x++){
        colToSort[x-1] = setDataType(allTR[x].childNodes[col].innerText)
        colArr[x-1] = allTR[x]
    }

    for (x=0; x<colToSort.length; x++){
        copyArr[x] = colToSort[x]
    }


    if (allNums){
        colToSort.sort(numberOrder)
    }
    else if (allDates){
        colToSort.sort(dateOrder)
    }
    else{
        colToSort.sort(textOrder)
    }


    for(x=0; x<colToSort.length; x++){
        for(y=0; y<copyArr.length; y++){
            if (colToSort[x] == copyArr[y]){
                boolListed = false
                //searcg the ouput array to make sure not to use duplicate rows
                for(z=0; z<resultArr.length; z++){
                    if (resultArr[z]==y){
                        boolListed = true
                        break;
                    }
                }
                if (!boolListed){
                    resultArr[x] = y
                    break;
                }
            }
        }
    }

    for (x=0; x<resultArr.length; x++){
        allTR[x+1].swapNode(colArr[resultArr[x]])
    }
}

function numberOrder(a,b){
    absOrder ? rVal = b - a : rVal = a - b
    return rVal
}

function dateOrder(a,b){
    absOrder ? rVal = Date.parse(a) - Date.parse(b) : rVal = Date.parse(b) - Date.parse(a)
    return rVal
}

function textOrder(a,b){
    if (a.toString() < b.toString()){
        absOrder ? rVal = -1 : rVal = 1
    }
    else{
        absOrder ? rVal = 1 : rVal = -1
    }
    return rVal
}
</script>
<body style="background-color:AliceBlue">
<h1 style="color:Blue" ><b> Employee Birthday Information </b></h1>
<br/>
<br/>

<h><b> Sort by : </b></h>

<input type="radio"  onclick="sortTable(0)" name=sort> Employee Id </input>
<input type="radio"  onclick="sortTable(1)" name=sort> Employee Name </input>
<input type="radio"  onclick="sortTable(2)" name=sort> Employee Birthdate </input>

</br>
</br>
<table id="dataTable"  align="center" cellpadding="10" cellspacing="20" bgcolor="LightCyan">
<tr>
    <th>Employee Name</th>
    <th>Employee Id</th>
    <th>Employee Birthdate</th>

</tr>
<td> 1 </td>
<td> Smita  </td>
<td> 4 Nov 1989</td>
</tr>
<tr>
<td> 26 </td>
<td> Priyanka </td>
<td> 29 Dec 1990 </td>
</tr>
<tr>
<td> 15 </td>
<td> Jack  </td>
<td> 7 Mar 1987 </td>
</tr>
<td> 4 </td>
<td> Joe </td>
<td> 4 June 1986 </td>
</tr>
<td> 48 </td>
<td> Julie </td>
<td> 3 July 1989 </td>
</tr>
</table>
4

2 回答 2

3
var now = new Date();

now.format("dd mmm");

在此处查看更多示例

于 2012-07-26T06:17:38.677 回答
0

从...开始:

var s = '4 Nov 1989';

那么你想要:

var newFormat = s.match(/\d+ \w{3}/)[0];

或(也许更健壮):

var newFormat = s.replace(/\s+\d+$/,'');
于 2012-07-26T07:00:23.270 回答