7

I want to change the date for of data which is in format 2016-10-15 to d-m-yy format in jquery as 15-10-2016.I tried and console the output it shows 2016-10-15.I caught this result in jquery ajax page which is fetched from database.

$.each(req,function(i,item){
    var val=$.format.date(req[i].from_date, "dd/MMM/yyyy");
    console.log(val);   //'2016-10-15'
});
Mohammad
  • 21,175
  • 15
  • 55
  • 84
user3386779
  • 6,883
  • 20
  • 66
  • 134
  • You can get it from here, http://stackoverflow.com/questions/20946475/how-to-change-date-format-using-jquery – Mr. J Oct 25 '16 at 05:54

6 Answers6

23

You can do this work use native javascript functions. Use .split() to split date by - delimiter into array and invert array using .reverse() and convert array to sting using .join()

var date = "2016-10-15";
date = date.split("-").reverse().join("-");
console.log(date);
Mohammad
  • 21,175
  • 15
  • 55
  • 84
3

You can do in javascript like this:

var dateAr = '2016-10-15'.split('-');
var newDate = dateAr[1] + '-' + dateAr[2] + '-' + dateAr[0];

console.log(newDate);
Manh Nguyen
  • 930
  • 5
  • 12
3

Easy with a regex .replace():

var input = "2016-10-15";
var output = input.replace(/(\d{4})-(\d\d)-(\d\d)/, "$3-$2-$1");
console.log(output);

You can easily allow the regex to accept several delimeters:

var re = /(\d{4})[-. \/](\d\d)[-. \/](\d\d)/;

console.log("2015-10-15".replace(re, "$3-$2-$1"));
console.log("2015.10.15".replace(re, "$3-$2-$1"));
console.log("2015 10 15".replace(re, "$3-$2-$1"));
console.log("2015/10/15".replace(re, "$3-$2-$1"));
nnnnnn
  • 147,572
  • 30
  • 200
  • 241
1

You can try like this.

 var OldDate = new Date('2016-10-15');
 var NewDate = OldDate.getDate() + '-' + (OldDate.getMonth() + 1) + '-' + OldDate.getFullYear();
Bharat
  • 2,441
  • 3
  • 24
  • 36
1

One way to do this :

    var MyDate = '2016-10-15';
    var formattedDate = new Date(MyDate);
    var d = formattedDate.getDate();
    var m =  formattedDate.getMonth();
    m += 1;  // JavaScript months are 0-11
    var y = formattedDate.getFullYear();
    alert(d + "-" + m + "-" + y);

Working Fiddle

4b0
  • 21,981
  • 30
  • 95
  • 142
1

You can get date in many formats you refer below code.

var dateObj = new Date();
var div = document.getElementById("dateDemo");

div.innerHTML = "Date = " + dateObj.getDate() + 
"<br>Day = " + dateObj.getDay() + 
"<br>Full Year = " + dateObj.getFullYear() +
"<br>Hour = " + dateObj.getHours() +
"<br>Milli seconds = " + dateObj.getMilliseconds() + 
"<br>Minutes = " + dateObj.getMinutes() + 
"<br>Seconds = " + dateObj.getSeconds() + 
"<br>Time = " + dateObj.getTime();
<div id="dateDemo"></div>

format the date as you want.

Jazzzzzz
  • 1,593
  • 15
  • 16