Reputation: 1526
From the ajax, I am receiving datetime as:
Not sure now to parse it with jquery to "dd/MM/yyyy"
Upvotes: 2
Views: 12119
Reputation: 6575
Since you posted your question in the jquery tag, you could embed your approach in a jQuery
function:
//returns a Date() object in dd/MM/yyyy
$.formattedDate = function(dateToFormat) {
var dateObject = new Date(dateToFormat);
var day = dateObject.getDate();
var month = dateObject.getMonth() + 1;
var year = dateObject.getFullYear();
day = day < 10 ? "0" + day : day;
month = month < 10 ? "0" + month : month;
var formattedDate = day + "/" + month + "/" + year;
return formattedDate;
};
//usage:
var formattedDate = $.formattedDate(someDateObjectToFormat);
This will work with both valid Date
objects and the result you get back from your JSON string:
var randomDate = new Date("2015-09-30");
var epochTime = new Date(1263183045000);
var jsonResult = "\/Date(1263183045000)\/";
//outputs "30/09/2015"
alert($.formattedDate(randomDate));
//both below output "11/01/2010"
alert($.formattedDate(epochTime));
alert($.formattedDate(new Date(parseInt(jsonResult.substr(6)))));
Aside: the format of the date portion in your result is in Unix time—aka Epoch time—i.e. the number of seconds elapsed after 01/01/1970 00:00:00 UTC.
Upvotes: 4
Reputation: 10363
My solution:
// convert MVC datetime (e.g. "\/Date(628318530718)\/") into JavaScript Date
value = new Date(parseInt(value.replace("/Date(", "").replace(")/",""), 10));
Upvotes: 1
Reputation: 1526
Got Solved with following code
var date = new Date(parseInt(data.UPDATED_ON.substr(6)));
alert(date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear());
Upvotes: 1