Convert Months into Numbers (Jul > 07) and store YYYYMMDD

by Faisal Khan Janjua

HTML

<h3>Convert Months into Numbers (Jul > 07) and store YYYYMMDD</h3>
<h4>For date sorting in datatables, use display:none for hiding new dates </h4>

<table>
  <thead>
    <tr>
      <th>Date</th>
      <th>Method</th>
      <th>Amount</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>31 Jan 2018</td>
      <td>Easyload</td>
      <td>500</td>
    </tr>
    <tr>
      <td>02 Feb 2018</td>
      <td>Credit / Debit Card</td>
      <td>2200</td>
    </tr>
    <tr>
      <td>01 Oct 2016</td>
      <td>EasyPaisa Mobile Account</td>
      <td>3200</td>
    </tr>
    <tr>
      <td>01 Dec 2017</td>
      <td>EasyPaisa Mobile Account</td>
      <td>4000</td>
    </tr>
  </tbody>
</table>

JavaScript

$(document).ready(function(){
	function sortdate(){
		jQuery('table tbody tr').each(function(i){
			var oldDate = jQuery(this).find('td').eq(0).text();
			var date = oldDate.split(" ");
			var month = '0'+("JanFebMarAprMayJunJulAugSepOctNovDec".indexOf(date[1]) / 3 + 1);
			if(month.length>2){
				month = month.substr(1);
			}
			var span = '<span class="datehide">'+date[2]+month+date[0]+'</span> - ';
			jQuery(this).find('td').eq(0).prepend(span);
		});
	}
  sortdate();
});