简体   繁体   中英

What is the best way to display date time using Linq to Datatable?

I have a linq that select some column with dates to datatable. I am using ASP MVC 5 and Datatble.

I really need the DateTime. I have displayed string and had no issues. But when I try to display the raw date time I get this output

/Date(1569366000000)/   /Date(1569397607057)/

Here is my code

 var IQueryabletimesheet = (from expense in _context.ExpenseModel
                                       join expenseaudittb in _context.ExpenseAuditTB on expense.ExpenseID equals expenseaudittb.ExpenseID
                                       join project in _context.ProjectMaster on expense.ProjectID equals project.ProjectID
                                       join registration in _context.Registration on expense.UserID equals registration.RegistrationID
                                       join AssignedRolesAdmin in _context.AssignedRoles on registration.RegistrationID equals AssignedRolesAdmin.RegistrationID
                                       where AssignedRolesAdmin.AssignToAdmin == UserID && expenseaudittb.Status == 3
                                       select new ExpenseModelView
                                       {
                                           ExpenseID = expense.ExpenseID,
                                           ProjectName = project.ProjectName,
                                           RequestBy = registration.Name,
                                           FromDate = expense.FromDate.Value,

                                           ProcessedDate = expenseaudittb.ProcessedDate,

                                           ExpenseStatus = expense.ExpenseStatus == 1 ? "Submitted" : expense.ExpenseStatus == 2 ? "Approved" : expense.ExpenseStatus == 4 ? "Pending Final Approval" : "Rejected",

                                           PurposeorReason = expense.PurposeorReason,
                                           TotalAmount = expense.TotalAmount,

                                           VoucherID = expense.VoucherID,
                                       });

            if (!(string.IsNullOrEmpty(sortColumn) && string.IsNullOrEmpty(sortColumnDir)))
            {
                IQueryabletimesheet = IQueryabletimesheet.OrderBy(sortColumn + " " + sortColumnDir);
            }
            if (!string.IsNullOrEmpty(Search))
            {
                //IQueryabletimesheet = IQueryabletimesheet.Where(m => m.FromDate == Search);
            }

            return IQueryabletimesheet;

I am getting the result via ajax call in the view. That has no issue, except for the datetime columns that display the above errors

I need something like what I see in my SQL Server 08/10/2019 10:43

To display as expected you have to do the date to string conversion. ex. string.Format ("{0: G}", expense.FromDate.Value).

var IQueryabletimesheet = (from expense in _context.ExpenseModel
                                       join expenseaudittb in _context.ExpenseAuditTB on expense.ExpenseID equals expenseaudittb.ExpenseID
                                       join project in _context.ProjectMaster on expense.ProjectID equals project.ProjectID
                                       join registration in _context.Registration on expense.UserID equals registration.RegistrationID
                                       join AssignedRolesAdmin in _context.AssignedRoles on registration.RegistrationID equals AssignedRolesAdmin.RegistrationID
                                       where AssignedRolesAdmin.AssignToAdmin == UserID && expenseaudittb.Status == 3
                                       select new ExpenseModelView
                                       {
                                           ExpenseID = expense.ExpenseID,
                                           ProjectName = project.ProjectName,
                                           RequestBy = registration.Name,
                                           FromDate = string.Format ("{0:G}", expense.FromDate.Value),

                                           ProcessedDate = expenseaudittb.ProcessedDate,

                                           ExpenseStatus = expense.ExpenseStatus == 1 ? "Submitted" : expense.ExpenseStatus == 2 ? "Approved" : expense.ExpenseStatus == 4 ? "Pending Final Approval" : "Rejected",

                                           PurposeorReason = expense.PurposeorReason,
                                           TotalAmount = expense.TotalAmount,

                                           VoucherID = expense.VoucherID,
                                       });

I found a solution to my problem. Note, this may not be the only solution but does exactly what I want. The solution is using moment.js and this is how I rendered the column and everything worked. I wanted the data raw with its type (datetime) retained.

{
   title: "ProcessedDate",// name 
   render: function (data, type, row) 
            {//data
                        return moment(row.ProcessedDate).format('DD/MM/YYYY hh:mm:ss');
            }
}           

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM