How to convert date dd mon yyyy to mon dd format in javascript? I have created a table using javascript. The table contain date column in format dd mon yyyy. I want to convert that format to dd mon and sort it according to month. what to do? plz help me.
Here is my code...
<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>
You want to sort the dates by the month rigth?
Xmas95 = new Date("December 25, 1995 23:15:00")
month = Xmas95.getMonth()
This will give you the month.
See the mdn content: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getMonth
var a = new Date;
a.getDate(); // will give u date
a.getMonth(); // will give u month
a.getYear(); // will give u year
you can also get hour, seconds, minutes everything using .getHours() and so on.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.