Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello,

in my Controller, json result returns start date as 04/04/2017 but when I print start date in AJAX call return in view it displays as
/Date(1488133800000)/


I manipulated start date as below but it prints 4/4/2017.I need to print 04/04/2017.

how can I do that?

$.ajax({
url: '@Url.Action("JobsServiceProviderC", "PostWork")',
type: "Post",
dataType: "json",
data: { KeywordSearch: strKeywordSearch },
success: function (data) {
$("#tblJobs > tbody").html("");
if (data.data.length > 0) {
for (var i = 0; i < data.data.length; i++) {
tr = $('<tr/>');
var SDate = new Date(eval('new' + data.data[i].StartDate.replace(/\//g, ' ')));
var formattedSDate = SDate.getMonth() + 1 + '/' + SDate.getDate() + '/' + SDate.getFullYear();
<pre>$('#tblJobs > tbody').append(tr);
}},
error: function (jqXHR, textStatus, errorThrown) {
 $(".loading").css("display", "none");
}
});





Thanks

What I have tried:

var SDate = new Date(eval('new' + data.data[i].StartDate.replace(/\//g, ' ')));
                        var formattedSDate = SDate.getMonth() + 1 + '/' + SDate.getDate() + '/' + SDate.getFullYear();

tr.append("<td>" + formattedSDate + "</td>");
Posted
Updated 4-Apr-17 19:14pm
v2

try
var str = '4/4/2017';
  var parts = str.split('/');
  var month = ('0' + parts[0]).slice(-2);
  var day = ('0' + parts[1]).slice(-2);
  var year = parts[2];
  var result = month + '/' + day + '/' + year;
 
Share this answer
 
Comments
kkakadiya 5-Apr-17 1:17am    
Hello Karthik,
We can do like your solution but that is the not way to concate 0 as prefix. it is better if we can do it with date function.
Karthik_Mahalingam 5-Apr-17 1:23am    
through date function it is not possible, you will have to use external library.
what is the isssue in this.
kkakadiya 5-Apr-17 1:25am    
I mean From date function is to go with some generic way.
Issue is nothing with your solution, I have just tried with it and it works for me.
Karthik_Mahalingam 5-Apr-17 1:36am    
you shall make it as function and add a prototype to the date time object.
if it works, pls close this post.
kkakadiya 5-Apr-17 2:56am    
Thanks Karthik,
I have done the same. made generic function calling from anywhere from the project and it works perfect.
tr.append("<td>" + SDate.ToString("dd-MM-yyyy"); + "</td>");
 
Share this answer
 
Comments
Richard Deeming 4-Apr-17 12:21pm    
That's not going to work in Javascript.
kkakadiya 5-Apr-17 1:03am    
Richard is right. it will not work in javascript especially when return format is json result.
Wessel Beulink 5-Apr-17 3:55am    
You guys right indeed, was thinking of C# because that was the title tag... my bad

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900