Alex Rivera | Logout

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

Asked 2010-01-18T14:46:20.770
24

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.

Edit
Report

2 Answers

23

Easiest way, assuming you're not bothered about the function being dynamic:

function reformatDate(dateStr)
{
  var dArr = dateStr.split("-");  // ex input: "2010-01-18"
  return dArr[2]+ "/" +dArr[1]+ "/" +dArr[0].substring(2); //ex output: "18/01/10"
}
answered 2010-01-18T14:56:23.857
2

You may also want to look into using date.js:

http://www.datejs.com

To futureproof your application, you may want to return time in a UTC timestamp and format with JavaScript. This'll allow you to support different formats for different countries (in the U.S., we are most familiar with DD-MM-YYYY, or instance) as well as timezones.

answered 2010-01-18T18:46:41.037

Your Answer