TheDarkCode
TheDarkCode

Reputation: 411

reverse date javascript from yyyy/mm/dd to dd/mm/yyyy

I making an asp project and the date is displayed as yyyy/mm/dd but when the user press on the edit button for the form i need to replace the date format with dd/mm/yyyy

this the asp code remember i need the user to show the date as yyyy/mm/dd in the table but when he pressing on a button i need the date to be reversed to dd/mm/yyyy

 <td class="hidden-phone">@EnnotaBAL.ArabicEncoding.GetArabicNumbers(transaction.PostDate.ToString("yyyy/MM/dd"))</td>

and this the javascript code for the date element:

// Transaction Date
var TransDate = oCells.item(4).innerHTML;
document.getElementById('Date').value = convertDigitIn(TransDate);

Upvotes: 9

Views: 18006

Answers (2)

Trev Stev
Trev Stev

Reputation: 1

My method:

function myFunction() {
  let text = document.getElementById("myDate").value;
  const myArray = text.split("-");
  document.getElementById("demo0").innerHTML = 'The new reversed Book date = ' + myArray[2] + '/' + myArray[1] + '/' + myArray[0];
  BookingDate = myArray[2] + '/' + myArray[1] + '/' + myArray[0];
  document.getElementById("demo1").innerHTML = 'the new variable BookingDate = ' + BookingDate;
}
Book Date: <input type="date" id="myDate" name="myDate" onfocusout="myFunction()">
<p id="demo0"></p>
<p id="demo1"></p>

Upvotes: 0

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382404

function convertDigitIn(str){
   return str.split('/').reverse().join('/');
}

Upvotes: 32

Related Questions