简体   繁体   English

使用 Linq 到 Datatable 显示日期时间的最佳方法是什么?

[英]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.我有一个 linq select 一些列,其中包含数据表的日期。 I am using ASP MVC 5 and Datatble.我正在使用 ASP MVC 5 和 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但是当我尝试显示原始日期时间时,我得到了这个 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.我通过视图中的 ajax 调用获得结果。 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我需要像我在 SQL 服务器 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). string.Format(“{0:G}”,费用.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.解决方案是使用 moment.js,这就是我呈现列的方式,一切正常。 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');
            }
}           

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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