10K35H 5H4KY4
10K35H 5H4KY4

Reputation: 1526

Parsing Datetime format passed from ajax to "dd/MM/yyyy"

From the ajax, I am receiving datetime as:

Screen Shot

Not sure now to parse it with jquery to "dd/MM/yyyy"

Upvotes: 2

Views: 12119

Answers (3)

trashr0x
trashr0x

Reputation: 6575

Since you posted your question in the 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

Maksym Kozlenko
Maksym Kozlenko

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

10K35H 5H4KY4
10K35H 5H4KY4

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

Related Questions