如何将日期时间固定为仅限日期

本文关键字:日期 时间 | 更新日期: 2023-09-27 17:58:14

这是我的视图

SELECT        AmenityValidDatesPriceID, AmenityID, AmenityValidDatesFrom, AmenityValidDatesTo, AddedDate, LastModifyDate, AddedUser, LastModifyUser, 
                         IsDeleted
FROM            dbo.DataManagement_Hotels_Amenities_AmenityValidDatesPrice
WHERE        (IsDeleted = 0)

它将AmenityValidDatesFrom,AmenityValidateDatesTo作为日期时间返回给我

在前端,我只需要日期

    public List<BLL.Entities.DataManagement_Hotels_Amenities_AmenityValidDatesPrice> SelectByAmenityID(int ID)
            {
                DAL.DynamicSearchViewDataContext Viewcontext = new DAL.DynamicSearchViewDataContext();
                DAL.SelectSectionDataContext SpContext = new DAL.SelectSectionDataContext();
                var Query = (from car in Viewcontext.View_DataManagement_Hotel_Amenities_Dates
                             where car.AmenityID.Equals(ID)
                             select new BLL.Entities.DataManagement_Hotels_Amenities_AmenityValidDatesPrice
                             {
                                 AmenityID = (int)car.AmenityID,
                                 //AmenityValidDatesFrom = (DateTime)car.AmenityValidDatesFrom,
                                 //AmenityValidDatesTo = (DateTime)car.AmenityValidDatesTo
                                 AmenityValidDatesFrom = ((DateTime)car.AmenityValidDatesFrom).Date,
                                 AmenityValidDatesTo = ((DateTime)car.AmenityValidDatesTo).Date

                             }).ToList<BLL.Entities.DataManagement_Hotels_Amenities_AmenityValidDatesPrice>();
                return Query;
            }

这是我的代码

它给出

错误隐式转换

如何将日期时间固定为仅限日期

试试这个

SELECT  AmenityValidDatesPriceID ,
        AmenityID ,
        CONVERT(VARCHAR(10), AmenityValidDatesFrom, 103) AS AmenityValidDatesFrom ,
        CONVERT(VARCHAR(10), AmenityValidDatesTo, 103) AS AmenityValidDatesTo ,
        CONVERT(VARCHAR(10), AddedDate, 103) AS AddedDate ,
        CONVERT(VARCHAR(10), LastModifyDate, 103) AS LastModifyDate ,
        AddedUser ,
        LastModifyUser ,
        IsDeleted
FROM    dbo.DataManagement_Hotels_Amenities_AmenityValidDatesPrice
WHERE   IsDeleted = 0

它将以"dd/mm/yyyy"格式给出日期。如果您想获得"mm/dd/yyyy"格式的日期,请使用101而不是103

请更改您的视图,如下所示:

SELECT  AmenityValidDatesPriceID,
        AmenityID,
        CONVERT(VARCHAR, AmenityValidDatesFrom,101) AS AmenityValidDatesFrom,
        CONVERT(VARCHAR, AmenityValidDatesTo,101) AS AmenityValidDatesTo,
        CONVERT(VARCHAR, AddedDate,101) AS AddedDate,
        CONVERT(VARCHAR, LastModifyDate,101) AS LastModifyDate,
        AddedUser,
        LastModifyUser,
        IsDeleted
FROM    dbo.DataManagement_Hotels_Amenities_AmenityValidDatesPrice
WHERE   IsDeleted = 0

它应该起作用。

更新:

需要进行null检查。

public List<BLL.Entities.DataManagement_Hotels_Amenities_AmenityValidDatesPrice> SelectByAmenityID(int ID)
{
    DAL.DynamicSearchViewDataContext Viewcontext = new DAL.DynamicSearchViewDataContext();
    DAL.SelectSectionDataContext SpContext = new DAL.SelectSectionDataContext();
    var Query = (from car in Viewcontext.View_DataManagement_Hotel_Amenities_Dates
                 where car.AmenityID.Equals(ID)
                 select new BLL.Entities.DataManagement_Hotels_Amenities_AmenityValidDatesPrice
                 {
                     AmenityID = (int)car.AmenityID,
                     if (car.AmenityValidDatesFrom != null && car.AmenityValidDatesFrom.ToString() !=  "")
                     {
                        AmenityValidDatesFrom = car.AmenityValidDatesFrom.Date
                     },
                     if (car.AmenityValidDatesTo != null && car.AmenityValidDatesTo.ToString() !=  "")
                     {
                        AmenityValidDatesTo = car.AmenityValidDatesTo.Date
                     }
                 }).ToList<BLL.Entities.DataManagement_Hotels_Amenities_AmenityValidDatesPrice>();
    return Query;
}

使用此视图来保留DATETIME数据类型,同时去掉时间部分:

SELECT  AmenityValidDatesPriceID ,
        AmenityID ,
        DATEADD(D, 0, DATEDIFF(D, 0, AmenityValidDatesFrom)) AS AmenityValidDatesFrom
        DATEADD(D, 0, DATEDIFF(D, 0, AmenityValidDatesTo)) AS AmenityValidDatesTo
        DATEADD(D, 0, DATEDIFF(D, 0, AddedDate)) AS AddedDate
        DATEADD(D, 0, DATEDIFF(D, 0, LastModifyDate)) AS LastModifyDate
        AddedUser ,
        LastModifyUser ,
        IsDeleted
FROM    dbo.DataManagement_Hotels_Amenities_AmenityValidDatesPrice
WHERE   IsDeleted = 0