This
article will show you how you can get only date from list containing datetime
using linq in c#.net. This article will show How to remove time portion of date
in C# in DateTime object only or How to get only Date from datetime.
Some of my
previous articles are as follows: C#
Dictionary Search Key By Value, C#
Dictionary Get Value By Key, How
to Extract Text From PDF File Using C#.Net, How
to Read a Text File Line by Line in C# in Console Application.
So for this
article first we will create a new console application and add the below code.
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Text;
using
System.Threading.Tasks;
namespace
ConsoleApplication2
{
class Program
{
/// <summary>
/// Get only date from list
using linq in c#
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
List<DateTime> dateTime = new List<DateTime>();
dateTime = GetDateList();
var onlyDateList =
dateTime.Select(m => m.Date.ToString("dd/MM/yyyy"));
foreach(var item in onlyDateList)
{
Console.WriteLine(item + "\n");
}
Console.ReadLine();
}
public static List<DateTime> GetDateList()
{
List<DateTime> dateTime = new List<DateTime>();
dateTime.Add(DateTime.Now.AddDays(1));
dateTime.Add(DateTime.Now.AddDays(2));
dateTime.Add(DateTime.Now.AddDays(3));
return dateTime;
}
}
}
|
In above code I have prepared a list of datetime. Now I have applied linq query to remove time from the date.
0 comments:
Please let me know your view