Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript function to convert date yyyy/mm/dd to dd/mm/yy

I am trying to create a function on javascript to bring the date from my database in format (yyyy-mm-dd) and display it on the page as (dd/mm/yy).

I would appreciate any help.

Thanks.

PD: Let me know if you need more clarification.

like image 779
Amra Avatar asked Jan 18 '10 14:01

Amra


1 Answers

If you're sure that the date that comes from the server is valid, a simple RegExp can help you to change the format:

function formatDate (input) {
  var datePart = input.match(/\d+/g),
  year = datePart[0].substring(2), // get only two digits
  month = datePart[1], day = datePart[2];

  return day+'/'+month+'/'+year;
}

formatDate ('2010/01/18'); // "18/01/10"
like image 107
Christian C. Salvadó Avatar answered Oct 03 '22 17:10

Christian C. Salvadó