简体   繁体   English

如何从 Linq c# 中的日期时间中删除日期

[英]how to remove date from datetime in Linq c#

i want to get only time from a datetime variable in linq query.我只想从 linq 查询中的日期时间变量中获取时间。 for eaxample if i have an array of datetime having values例如,如果我有一个包含值的日期时间数组

["02/12/1970 14:52:06","14/06/2015 12:32:44"]

then how to extract minimum time from it so that i get output like那么如何从中提取最短时间,以便我得到 output 之类的

"12:32:44" 

as if i take minimum these two it will give output "14:52:44" instead of "12:32:44" as the datepart of first value is smaller than the other.好像我至少取这两个它会给 output "14:52:44"而不是"12:32:44"因为第一个值的日期部分小于另一个值。

i try this code:我试试这个代码:

time1 = table1.Min(x=>x.StartTime)

but it will give "14:52:06" as output但它会给“14:52:06”作为 output

any suggestions?有什么建议么?

First extract Time from DateTime and then get Min value首先从 DateTime 中提取时间,然后获取最小值

var dates = new List<DateTime>
{
    new DateTime(2020, 6, 9, 12, 35, 45),
    new DateTime(2020, 6, 10, 11, 35, 45)
};

var minTime = dates
    .Select(d => d.TimeOfDay)
    .Min();

#Update #更新

If you want to get DateTime object that has the smallest time part, then use this:如果要获取具有最小time部分的 DateTime object,请使用以下命令:

var dateWithMinTime = dates
    .OrderBy(d => d.TimeOfDay)
    .FirstOrDefault();

Try尝试

table1.Min(c => c.StartTime.TimeOfDay)

assuming StarTime is a DateTime .假设StarTimeDateTime

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

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