8

What I am doing is, fetching data from database using ajax and show it on html text boxes for update purposes. Below is my Web Method code from where I get data 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 database I got date in this format /Date(725828400000)/. I search google about parse and converting json date string format into html / javascript date also use 3rd party plugins like moment.js and jquery.ui but not solve my problem. Also here I am sharing my code from which I get data from ajax in json format and show 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 want date in mm/dd/yyyy format. Please help me to solve my prob.

Ahmer Ali Ahsan
  • 5,636
  • 16
  • 44
  • 81

5 Answers5

6

If there is no issue in adding a dependency, then you can add moment.js and it will help you to format data in any format I am supposing that date from 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 are unable to add moment js then you can do soemthing 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);
Zohaib Ijaz
  • 21,926
  • 7
  • 38
  • 60
  • @charlietfl I edited my answer, I thought he is getting a valid date string but format is different – Zohaib Ijaz Feb 07 '16 at 12:23
  • Thanks for your right answer. Its works me. But Here Is one problem also which is ` ` Moment js not access into my ajaxcall external file. so I put ajaxcall --> getEmployee() method outside from the external file so easily I can access it from it. But I want to access my moment() into my ajaxcall method cause in that method I put all ajax method. – Ahmer Ali Ahsan Feb 07 '16 at 12:42
  • your previous code shows me correct format. Why you edited it? Actually the main problem is moment() method is not accessible into my ajaxcall.js file. I also put moment.js file reference before but its not access into ajaxcall. If you tell me how can I reference moment.js into ajaxcall.js external file. So I am very thankful to you again. – Ahmer Ali Ahsan Feb 07 '16 at 12:50
  • actually @charlietfl down voted that answer , if you are unable to insert moment js then you can create a function to format your date – Zohaib Ijaz Feb 07 '16 at 12:52
  • What is d? aur if i put `jsonObject.EMP_DOB.match` there is no intellisense matches with `match` – Ahmer Ali Ahsan Feb 07 '16 at 13:37
  • sorry, I was testing in console by assigning date value to `d` and I forgot to change the variable name – Zohaib Ijaz Feb 07 '16 at 13:44
  • Thanks @ZohaibIjaz your code works fine and get me out of my problem. – Ahmer Ali Ahsan Feb 07 '16 at 13:59
  • @AhmerAliAhsan but there is an edge case for `date.getMonth() + 1)`, if month is less that 10, you will get single digit. – Zohaib Ijaz Feb 07 '16 at 14:01
  • you means like : 1/9/2016 not like: 01/09/2016 – Ahmer Ali Ahsan Feb 07 '16 at 14:39
  • yeah, exactly.. I modified my answer to handle that – Zohaib Ijaz Feb 07 '16 at 15:12
0

Easiest way to do is 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 wil be able to format it whatever you want using C#

Mir Gulam Sarwar
  • 2,588
  • 2
  • 25
  • 39
0

You already have a data object, which has its methods to extract everything you need.

You can make a function like in this example:

Get string in yyyymmdd format from js date object

Community
  • 1
  • 1
xabitrigo
  • 1,341
  • 11
  • 24
0

this method will convert all your WCF type dates 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]);
    }
}
Morteza Tourani
  • 3,506
  • 5
  • 41
  • 48
-1
var dbDate = "2014/03/12";    
var date    = new Date(dbDate); 

// in place of hardcoded date place your string

For your case you are getting a long value as a string from Db. Add one more line

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

Now pass this value to your object. Like

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)
zakaiter
  • 439
  • 3
  • 11
  • I am getting date into `/Date(725828400000)/` this format not in this "2014/03/12" – Ahmer Ali Ahsan Feb 07 '16 at 12:57
  • var date = new Date(item.EMP_DOB) // its shows me NaN error – Ahmer Ali Ahsan Feb 07 '16 at 12:59
  • try this: var newDate = parseInt("725828400000"); // use here your item.EMP_DOB; Now pass this value to your object. Like var date = new Date(newDate); You are passing long value as a string hence you are not able to parse. – zakaiter Feb 08 '16 at 04:53