Reputation: 3298
I have a list of objects in my collection and need to format the date on an object date property (NoteDate) to show dates in the format like "dd'/'MM'/'yyyy HH:mm:ss"
whereas in database the format of the date is like '2015-02-19 00:00:00.000'
. Below is my object
public class Note
{
public int Id { get; set; }
public string NoteText { get; set; }
public DateTime? NoteDate { get; set; }
}
and I populate the collection as below
var notesList= _uow.Find<Note>(n => n.FK == leadId).ToList();
how can we write the query to get the desired date format? thanks
Upvotes: 0
Views: 1302
Reputation: 511
you should not modify the content of your buisness object just for a Show purpose, You should use a converter or a StringFormat like :
<TextBlock Text="{Binding Date, StringFormat={}{0:MM/dd/yyyy}}" />
see this question for more info
Upvotes: 0
Reputation: 33738
You are, properly, storing the date in a DateTime?
object. DateTime
is simply a storage mechanism.
What you are really interested in is how to display the DateTime
in some UI.
So here's the steps your Date/Time is going to take:
DateTime
property To format a value from a DateTime
object there are several options - check out the methods on the DateTime class.
Upvotes: 1
Reputation: 65049
Your dates will be represented as a DateTime
instance .. how .NET decides to represent dates.
When you're displaying them to your user/using them for display somewhere, you can simply format them at that point. For example:
var notesList = _uow.Find<Note>(n => n.FK == leadId).ToList();
var thirdNoteDateString = notesList[2].NoteDate.ToString("dd MM yyyy");
Or, perhaps in a Razor view (if this was an MVC application):
@foreach (var n in Model.Notes) {
<p>@n.NoteDate.ToString("dd MM yyyy")</p>
}
Hopefully that shows the difference. How the date is presented is up to you when you decide to present it.
Upvotes: 0