Hi kas12345,
You have to set JsonSerializerSettings.DateFormatString to your desired format.
Check this example. Now please take its reference and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
<table class="tblEmployees">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>BirthDate</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$.ajax({
type: "POST",
url: "Default.aspx/GetEmployees",
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var employees = JSON.parse(response.d)
var rows = '';
for (var i = 0; i < employees.length; i++) {
var id = employees[i].EmployeeId;
var name = employees[i].FirstName;
var dob = employees[i].DOB;
rows += "<tr><td>" + id + "</td><td>" + name + ' ' + "</td><td>" + dob + "</td></tr>";
}
$('.tblEmployees tbody').append(rows);
}
});
});
</script>
Namespaces
C#
using System.Data;
using System.Web.Services;
using System.Configuration;
using System.Data.SqlClient;
using Newtonsoft.Json;
VB.Net
Imports System.Data
Imports System.Web.Services
Imports System.Configuration
Imports System.Data.SqlClient
Imports Newtonsoft.Json
Code
C#
[WebMethod]
public static string GetEmployees()
{
string query = "SELECT EmployeeID,FirstName,BirthDate FROM Employees";
string constr = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
List<EmployeeModel> employees = new List<EmployeeModel>();
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
employees.Add(new EmployeeModel
{
EmployeeId = Convert.ToInt32(sdr["EmployeeID"]),
FirstName = sdr["FirstName"].ToString(),
DOB = Convert.ToDateTime(sdr["BirthDate"])
});
}
}
con.Close();
var jsonSettings = new JsonSerializerSettings();
jsonSettings.DateFormatString = "dd/MM/yyyy";
return JsonConvert.SerializeObject(employees, jsonSettings);
}
}
}
public class EmployeeModel
{
public int EmployeeId { get; set; }
public string FirstName { get; set; }
public DateTime? DOB { get; set; }
}
VB.Net
<WebMethod>
Public Shared Function GetEmployees() As String
Dim query As String = "SELECT EmployeeID,FirstName,BirthDate FROM Employees"
Dim constr As String = ConfigurationManager.ConnectionStrings("conString").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand(query)
Dim employees As List(Of EmployeeModel) = New List(Of EmployeeModel)()
cmd.CommandType = CommandType.Text
cmd.Connection = con
con.Open()
Using sdr As SqlDataReader = cmd.ExecuteReader()
While sdr.Read()
employees.Add(New EmployeeModel With {
.EmployeeId = Convert.ToInt32(sdr("EmployeeID")),
.FirstName = sdr("FirstName").ToString(),
.DOB = Convert.ToDateTime(sdr("BirthDate"))
})
End While
End Using
con.Close()
Dim jsonSettings = New JsonSerializerSettings()
jsonSettings.DateFormatString = "dd/MM/yyyy"
Return JsonConvert.SerializeObject(employees, jsonSettings)
End Using
End Using
End Function
Public Class EmployeeModel
Public Property EmployeeId As Integer
Public Property FirstName As String
Public Property DOB As DateTime?
End Class
Screenshot
Refer below links for more details.
Serialize DateFormatString
Serializing Dates in JSON