如何使用javascript根据表中的月份对日期列进行排序?
如何在javascript中将日期dd mon yyyy转换为mon dd格式? 我已经使用javascript创建了一个表。该表包含格式为dd mon yyyy的日期列。我想将该格式转换为dd mon,并根据月份对其进行排序。怎么办?请帮帮我 这是我的密码如何使用javascript根据表中的月份对日期列进行排序?,javascript,Javascript,如何在javascript中将日期dd mon yyyy转换为mon dd格式? 我已经使用javascript创建了一个表。该表包含格式为dd mon yyyy的日期列。我想将该格式转换为dd mon,并根据月份对其进行排序。怎么办?请帮帮我 这是我的密码 <script> var now = new Date(); now.format("dd mmm"); var allNums = false; var allDates = false; var lastSort = -
<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
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" align="center"><div id="header" style="background-color:LightCyan;"> Employee Birthday Information </div></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="10" 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> Julie </td>
<td> 29 Dec 1990 </td>
</tr>
<tr>
<td> 15 </td>
<td> Rashmi </td>
<td> 7 Mar 1987 </td>
</tr>
<td> 4 </td>
<td> Jack </td>
<td> 4 June 1986 </td>
</tr>
<td> 48 </td>
<td> Soniya </td>
<td> 3 July 1989 </td>
</tr>
<td> 32 </td>
<td> John </td>
<td> 3 June 1989 </td>
</tr>
<td> 45 </td>
<td> Prajakta </td>
<td> 9 Aug 1989 </td>
</tr>
<td> 7 </td>
<td> Apeksha </td>
<td> 6 Feb 1989 </td>
</tr>
</table>
var now=新日期();
格式(“dd-mmm”);
var-allNums=false;
var-allDates=false;
var lastSort=-1;
var吸收器=真;
函数setDataType(无效){
var isDate=新日期(无效);
如果(isDate==“NaN”){
如果(isNaN(无效)){
inValue=inValue.toUpperCase();
allNums=false
allDates=false
返回无效;
}
否则{
allDates=false
返回浮点值(1*无效);
}
}
否则{
allNums=false
返回无效;
}
}
函数可排序(col){
if(lastSort==col){
吸收器?吸收器=假:吸收器=真
}
否则{
吸收器=真
}
lastSort=col
allTR=document.getElementById(“数据表”).childNodes[0]。childNodes
totalRows=allTR.length
colToSort=新数组()
colArr=新数组()
copyArr=新数组()
resultar=新数组()
allNums=true
allDates=true
对于(x=1;x 对于(x=0;x您想按月份对日期排序吗
Xmas95 = new Date("December 25, 1995 23:15:00")
month = Xmas95.getMonth()
这将给你一个月的时间
请参阅mdn内容:
您还可以使用.getHours()等工具获取小时、秒、分钟等所有信息。但我不想获取月份。我只想按月份对日期列进行排序。但我不想获取月份。我只想按月份对日期列进行排序。。
var a = new Date;
a.getDate(); // will give u date
a.getMonth(); // will give u month
a.getYear(); // will give u year