How to parse a JSON date string in a date format

What I am doing is getting data from the database using ajax and displaying it in html text fields for update purposes. Below is my web method code where I get the data from successfully.

[WebMethod] public static List<Employee> getEmployee() { var slist = new List<Employee>(); var db = new BLUEPUMPKINEntities(); slist = db.Employees.ToList(); return slist; } 

Now, when I get data from a database, I got a date in this format /Date(725828400000)/ . I am looking for google about parsing and converting json date string format to html / javascript date also use third-party plugins like moment.js and jquery.ui, but does not solve my problem. Also here I am sharing my code, from which I get data from ajax in json format and display it on jquery datatable.

 $.ajax({ url: "Employees.aspx/getEmployee", data: null, contentType: "Application/json; charset=utf-8", responseType: "json", method: "POST", success: function (response) { //alert(response.d); var jsonObject = response.d; var result = jsonObject.map(function (item) { //var date = new Date(item.EMP_DOB); //var obj = Date.parse(date); var result = []; result.push(''); result.push(item.EMP_FNAME); result.push(item.EMP_MNAME); result.push(item.EMP_LNAME); result.push(item.EMP_EMAIL); result.push(item.EMP_DOB); //this is my date column in my database from where date is in yyyy/mm/dd format result.push(item.EMP_USERNAME); result.push(item.EMP_PASSWORD); result.push(item.ID); return result; }); myTable.rows.add(result); // add to DataTable instance myTable.draw(); }, error: function (xhr) { alert(xhr.status); }, Failure: function (response) { alert(response); } }); 

I need a date in the format mm / dd / yyyy. Please help me solve my problem.

+7
json javascript jquery ajax
source share
5 answers

If there is no problem in adding dependencies, you can add moment.js and this will help you format the data in any format. I assume that the date from the server is in this format '/Date(725828400000)/'

 var d = item.EMP_DOB; result.push(moment(Number(d.match(/\d+/)[0])).format('MM/DD/YYYY')); 

If you cannot add js moment then you can do something like

 var date = new Date(Number(d.match(/\d+/)[0])); var day = date.getDate(); day = day = (day < 10) ? ("0" + day) : day; var month = date.getMonth() + 1); month = (month < 10) ? ("0" + month) : month; var dateStr = day + "-" + month + "-" + date.getFullYear(); result.push(dateStr); 
+5
source share

The easiest way to do it below (no third-party js required)

 var data =from row in db.Employees.ToList() select new { EMP_DOB=row.EMP_DOB.ToString(), row.EMP_FNAME,row.EMP_MNAME,row.EMP_LNAME row.EMP_EMAIL,row.EMP_DOB,row.EMP_USERNAME,row.EMP_PASSWORD,row.ID }; 

If you want to format the date you can use

 var data =from row in db.Employees.ToList() select new { EMP_DOB=Convert.ToString(row.EMP_DOB).ToShortDateString(), other properties goes here as shown previously }; 

You can format it however you want with C #

0
source share

You already have a data object that has its own methods for retrieving everything you need.

You can make a function like in this example:

Get yyyymmdd string from js date object

0
source share

this method converts all dates of type WCF to javascript Date object:

 var dateRegex = /^\/Date\((d|-|.*)\)[\/|\\]$/; function convertWCFStringDate(strDate) { var matched = dateRegex.exec(strDate); if (matched) { var parts = matched[1].split(/[-+,.]/); return new Date(parts[0] ? +parts[0] : 0 - +parts[1]); } } 
0
source share
 var dbDate = "2014/03/12"; var date = new Date(dbDate); 

// instead of hardcoded date, specify your string

In your case, you get a long value as a string from Db. Add another line

  var newDate = parseInt("725828400000"); // use here your item.EMP_DOB; 

Now pass this value to your object. how

 var date = new Date(newDate); var mm = (date.getMonth()+1)>9?(date.getMonth()+1):"0"+(date.getMonth()+1); var dd = date.getDate()>9?date.getDate():"0"+date.getDate(); var yyyy = date.getFullYear(); var newDate = mm+"/"+"/"+dd+"/"+yyyy; alert(dbDate+" converted to "+newDate) 
-one
source share

All Articles