Reputation: 1220
Getting abit of a problem I only know how to use DateTime to display my results like so:
{
string uri = "http://localhost:8002/Service/HireDate";
XDocument xDoc = XDocument.Load(uri);
var staff = xDoc.Descendants("HireDate")
.Select(n => new
{
StartDate = DateTime.Parse(n.Element("HireFromDate").Value), //this line
EndDate = DateTime.Parse(n.Element("HireToDate").Value), //this line
TotalDaysHired = n.Element("NumberOfDaysHired").Value,
})
.ToList();
dataGrid9.ItemsSource = staff;
}
However this outputs like this in my datagrid:
15/07/2012 00:00:00
27/07/2012 00:00:00
Is there a way to remove the time I just want the date?
Upvotes: 1
Views: 8768
Reputation: 64477
To get the date without time as a string, you can use ToShortDateString
or custom formatting options provided to ToString
:
var dateString = DateTime.Now.ToString("dd/MM/yyyy");
var shortDate = DateTime.Now.ToShortDateString();
Culture is considered in both ToShortDateString
and ToString
with a custom format (although the format is still your own). ToShortDateString
may be more accurate with the current culture as it contains the correct format for a given culture.
Upvotes: 4
Reputation: 3901
when you're putting the date time, you can use the ToString(string format) method to set the proper format.
here you can find few format smaple: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
StartDate = DateTime.Parse(n.Element("HireFromDate").Value).ToString("dd/MM/yyyy"),
EndDate = DateTime.Parse(n.Element("HireToDate").Value).ToString("dd/MM/yyyy")
Upvotes: 0
Reputation: 2041
You can use DateTime.ParseExact function.
e.g :
XDocument xDoc = XDocument.Load(uri);
var staff = xDoc.Descendants("HireDate")
.Select(n => new
{
StartDate = DateTime.ParseExact(n.Element("HireFromDate").Value, "dd/MM/yyyy", null), //this line
EndDate = DateTime.ParseExact(n.Element("HireToDate").Value, "dd/MM/yyyy", null), //this line
TotalDaysHired = n.Element("NumberOfDaysHired").Value,
})
.ToList();
dataGrid9.ItemsSource = staff;
Upvotes: 0
Reputation: 12874
EndDate = DateTime.Parse(n.Element("HireToDate").Value).ToShortDateString()
Upvotes: 0
Reputation: 460068
So you want to show only the Date
part of a DateTime
as String
? You could use DateTime.ToShortDateString()
which is most readable:
StartDate = DateTime.Parse(n.Element("HireFromDate").Value).ToShortDateString()
Upvotes: 6