0

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

rumi
  • 3,293
  • 12
  • 68
  • 109
  • 1
    you can change format at the time of displaying simply – Ehsan Sajjad Feb 20 '15 at 11:20
  • 1
    A `DateTime` is a value (a date), not a string (a representation of the value). If you want a string, you need a string. – xanatos Feb 20 '15 at 11:20
  • how do you present the dates? what does linq have to do with this? – default Feb 20 '15 at 11:28
  • Formatting at the front end is very obvious solution but the problem I' m having is I need to pass this list as a serialized json string to a jquery plugin which is not good at formatting at their end. So I'm trying to convert it to a format I need beforehand – rumi Feb 20 '15 at 11:29
  • Can you add details of how you convert it to json to your question? Do also state why the json parser would have issues parsing a `DateTime`. It seems this should be a common case of conversion. – default Feb 20 '15 at 11:34
  • In my controller I'm doing the conversion as `var notes = _leadsService.GetCallBackNotesByLeadId(id); return Json(JsonConvert.SerializeObject(notes), JsonRequestBehavior.AllowGet);` – rumi Feb 20 '15 at 11:35
  • 1
    add it to your question via the `edit` button. When you've done that it is Ok to also delete the comment. That keeps all of the important information in the question itself instead of users having to read the comments as well. – default Feb 20 '15 at 12:04

3 Answers3

1

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:

  1. Get returned as query content from the database
  2. Get stored in the DateTime property
  3. Be shown to the user

To format a value from a DateTime object there are several options - check out the methods on the DateTime class.

Sam Axe
  • 33,313
  • 9
  • 55
  • 89
0

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.

Simon Whitehead
  • 63,300
  • 9
  • 114
  • 138
0

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

Community
  • 1
  • 1
Yahya
  • 501
  • 4
  • 8