简体   繁体   中英

How to get only date from date-time through linq in c#?

I have this code:

 DataTable dt = DataAccess.GetDataInDataTable(ConnString, "sp_getHolidays", CommandType.StoredProcedure);
List<string> columnDates = dt.Columns.Cast<DataColumn>().Select(cols => cols.ColumnName).ToList();

This code brings me the list of dates in format: yyyy-MM-dd 00:00:00.000 .

But I want only in yyyy-MM-dd format. I don't want time. How to get it in the way I want? Or is it even possible through linq or any other way?

Your ColumnName is of type string not DateTime , Split ColumnName by space and select first element that is Date .

List<string> columnDates = dt.Columns.Cast<DataColumn>()
               .Select(cols => cols.ColumnName.Split(" ").First())
               .ToList();

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