简体   繁体   English

将日期时间转换为日期字符串“dd/mm/yyyy”

[英]Convert DateTime to Date string "dd/mm/yyyy"

How can I convert DateTime to a string "dd/mm/yyyy"?如何将 DateTime 转换为字符串“dd/mm/yyyy”? The problem I'm having is that since the conversion is inside a linq expression I must use DateTime.Value.ToString("MM/dd/yyyy") .我遇到的问题是,由于转换在 linq 表达式中,我必须使用DateTime.Value.ToString("MM/dd/yyyy") However, the date keeps coming back with the time-- 1/21/1999 12:00:00 AM .但是,日期会随着时间不断返回 - 1/21/1999 12:00:00 AM How can I remove the time part directly in this query?如何直接在此查询中删除时间部分?

_unitOfWork.PlacementFinancialViewRepository.GetEfs07Placements()
  .Where(x => x.FiscalYear == fiscalYear && x.ResponsibleSauId == responsibleDistrictId)
  .ToList()
  .Select(x => new Efs07PlacementModel
   {
     PlacementStartDate = x.PlacementStartDate == null 
       ? null 
       : x.PlacementStartDate.Value.ToString("MM/dd/yyyy"),
     PlacementEndDate = x.PlacementEndDate == null 
       ? null 
       : x.PlacementEndDate.Value.ToString("MM/dd/yyyy")
    })
  .ToList();

You may leave the List creation as is, with no conversion to the required format.您可以按原样保留列表创建,无需转换为所需格式。 So you may use you code as:所以你可以使用你的代码:

_unitOfWork.PlacementFinancialViewRepository.GetEfs07Placements().Where(x => x.FiscalYear == fiscalYear && x.ResponsibleSauId == responsibleDistrictId)
.ToList()
.Select(x => new Efs07PlacementModel
{
    PlacementStartDate = x.PlacementStartDate == null ? null : x.PlacementStartDate,
    PlacementEndDate = x.PlacementEndDate == null ? null : x.PlacementEndDate
}).ToList();

Then you can then convert the PlacementStartDate to your required format at the time yo uare displaying it on a View/UI.然后,您可以将PlacementStartDate转换为您在视图/UI 上显示时所需的格式。

A DateTime object is a count of milliseconds that have elapsed since midnight, January 1, 0001 AD. DateTime 对象是自公元 0001 年 1 月 1 日午夜以来经过的毫秒计数。 You are actually queering a number and automatically casting it into a DateTime (essentially).您实际上是在对一个数字进行奇怪的处理并自动将其转换为 DateTime(本质上)。 If your database has been saving the day but not the time, it will result in those milliseconds being set to 0. This is because you have just simply not added the milliseconds that represent the time of day.如果您的数据库一直在保存日期而不是时间,则会导致这些毫秒被设置为 0。这是因为您只是没有添加代表一天中的时间的毫秒。

You can, as others have stated, ignore the time or ensure that it is always set to midnight.正如其他人所说,您可以忽略时间或确保它始终设置为午夜。 Then in your displays set the format.然后在您的显示器中设置格式。

I would suggest doing this in your model object with System.ComponentModel.DataAnnotations .我建议使用System.ComponentModel.DataAnnotations在您的模型对象中执行此操作。

[DisplayFormat(DataFormatString = "{0:dd/mm/yyyy}", ApplyFormatInEditMode = true)]

This way you can use @Html.DisplayFor(x => x.DateTimeObject) in your view and that sets your formatting.这样您就可以在视图中使用@Html.DisplayFor(x => x.DateTimeObject)并设置格式。

Update:更新:

In addition, you can add the attribute [DataType(DataType.Date)] in your object model.此外,您可以在对象模型中添加属性[DataType(DataType.Date)] This will not change how the data is stored in the database by EF6, just how it is displayed by default.这不会改变 EF6 数据在数据库中的存储方式,只是默认情况下的显示方式。 Some other ORM's may not behave this way so please read up on your ORM.其他一些 ORM 可能不会以这种方式运行,因此请仔细阅读您的 ORM。

[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
[DataType(DataType.Date)]
public DateTime PlacementStartDate { get; set; }

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

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