0

I have an input text with id CldrFrom

It has date like this: 04/17/2014 in other words mm/dd/yyyy

I want to change it to this format 17-04-2014 in other words dd-mm-yyyy

I want to do that in jQuery.

What I have tried

var currentDate = $(#CldrFrom").val();
val = val.replace("/", "-");

So I replaced the / with - but I still need to make the day as the first not the month. how please?

Edit

to take the date I am using a datepicker library. I call it like this:

$("#CldrFrom, #CldrTo").datepicker();

is there anyway so directly I make that library prints dates in this format dd-mm-yyyy ?

MZaragoza
  • 10,108
  • 9
  • 71
  • 116
Marco Dinatsoli
  • 10,322
  • 37
  • 139
  • 253
  • if you use jQuery UI, you can do it with $.datepicker.formatDate('dd-MM-yyyy', new Date()) – bobthedeveloper Apr 15 '14 at 09:42
  • Possible duplicate of [Date Formatting](http://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript) –  Apr 15 '14 at 09:43

2 Answers2

1

Just split and join it together in the order you want

var currentDate = $("#CldrFrom").val();
var dateArr     = currentDate.split('/');
var val         = dateArr[1] + '-' + dateArr[0]  + '-' + dateArr[2];

FIDDLE

adeneo
  • 312,895
  • 29
  • 395
  • 388
0

You can provide a simple regex to .replace() method:

var currentDate = $("#CldrFrom").val(),
    val         = currentDate.replace(/\//g , "-");

Fiddle Demo

Felix
  • 37,892
  • 8
  • 43
  • 55