Naveen1425
Naveen1425

Reputation: 11

how to change date format in jquery date coming from a datatable

aData[8] is the column number which shows date in dd-mm-yyyy format.

I want to change it to yyyy-mm-dd format for validation purpose.

var dueDate = new Date(aData[8]);
console.log(aData[8]);
var dt_to = $.datepicker.formatDate('dd-mm-yy', new Date(dueDate));
var dueDatetimestamp = dt_to.getTime();

Upvotes: 1

Views: 942

Answers (4)

Kevin Labécot
Kevin Labécot

Reputation: 1995

Personnally, I'm using jQuery.format.date(value,'date-format'). But this is a jQuery's plugin available at https://github.com/phstc/jquery-dateFormat

Very simple to use (example from Github) :

<script>
  document.write($.format.date("2009-12-18 10:54:50.546", "Test: dd/MM/yyyy"));
  document.write($.format.date("Wed Jan 13 10:43:41 CET 2010", "dd~MM~yyyy"));
</script>

Upvotes: 0

cracker
cracker

Reputation: 4906

you can use date.js to achieve :

var dateAr = '2014-01-06'.split('-');
var newDate = dateAr[0] + '-' + dateAr[1] + '-' + dateAr[2];

DEMO

Upvotes: 0

Manwal
Manwal

Reputation: 23816

Consider this:

var str = '13-02-2014';//your format dd-mm-yyyy
var newar = str.split('-');
var tempDate = newar[2] + '-' + newar[1] + '-' + newar[0];
var d = new Date(tempDate);
console.log(d);//date object
console.log(tempDate);//new format yyyy-mm-dd

DEMO

Upvotes: 0

Marc Lainez
Marc Lainez

Reputation: 3080

I like to use moment.js to manipulate dates.

You could do something like that with it:

moment("13-08-2014", "DD-mm-YYYY").format("YYYY-mm-DD")

Upvotes: 1

Related Questions