-3

I have a date value that I want to strip the time from. I want the return type to be a date type since I want to order the list of date I have. having a list to string representing Date does not return a correct order. I know that DateTime always returns the date with the time. What are my options here? How can I better my code and have a list of items of Date type without the time?

Edit: I would like to have the date only. nothing after that. Something like 8/7/2016 not 8/7/2016 00:00:00 or anything after date. and in a date object.

Here is my code:

 using (var db = new MyModel.Context())
            {
                var cert = (

                    from tr in db.uspTranscripts(personId)
                    from a in db.PersonTranscripts.Where(x => x.UPID == personId)
                    from b in db.LU_CreditType.Where(x => x.ID == a.CreditTypeID)

                                    select new CertViewModel
                                    {

                                        ActivityTitle = tr.ActivityTitle,
                                        Score = tr.Score,
                                        Status = tr.Status,
                                        CompletionDate = tr.CompletionDate,
                                        CretitTypeName = b.ShortName,
                                        CompletedDateSorted = a.HK_CreatedOn

                                    }).OrderByDescending(x => x.CompletedDateSorted).ToList();

                List<CertViewModel> certlist = cert;

                foreach (var item in certlist)
                {
                    string itemWithoutHour = item.CompletionDate.Value.ToShortDateString();
                     var itemConverted = DateTime.ParseExact(itemWithoutHour, "M/d/yyyy", null);
                item.CompletionDate = itemConverted;

                }

                return certificateslist.GroupBy(x => x.ActivityTitle).Select(e => e.First()).ToList();
            }
Francky
  • 55
  • 1
  • 7
  • Duplicate, already answered in http://stackoverflow.com/questions/6121271/how-to-remove-time-portion-of-date-in-c-sharp-in-datetime-object-only – user319785 Aug 08 '16 at 06:01

2 Answers2

2

For any given DateTime object, you can reference its Date property to strip out the time values:

var withTime = DateTime.Now; // 8/7/2016 22:11:43
var withoutTime = withTime.Date; // 8/7/2016 00:00:00
David
  • 176,566
  • 33
  • 178
  • 245
  • But it still shows the time as 00:00:00. I would like something line 8/7/2016 and nothing after that. – Francky Aug 08 '16 at 02:23
  • 3
    @Francky: A `DateTime` object by itself doesn't *show* anything. How you *display* the value in a UI is a separate concern entirely. If you want to display it without a time, then display it without a time. But the `DateTime` type has those properties. If you want a type without those properties, I guess you'd need to define your own. – David Aug 08 '16 at 02:24
  • the whole thing is to be able to display it without the time. I can strip the time part but will have to convert it to string, hence we comeback to the same issue of having a Date, not a string. The main concern here is to be able to sort the list. converting it to string will return an incorrect sorted list. – Francky Aug 08 '16 at 02:34
  • @Francky: Again, you can *display* the value however you like. There are many options to format the output however you like: https://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx Just perform the sorting on the `DateTime` value, and when you display use any display string you like. This really sounds like a non-issue. – David Aug 08 '16 at 02:43
0

The .NET framework does not have a date-only object.

It may be worth understanding how the DateTime structure works. Internally, it stores an offset in ticks (1 tick = 100 nanoseconds) since 1/01/0001 12:00 am in a single 64-bit unsigned integer. (1 tick = 100 nanoseconds)

The DateTime structure then provides many useful methods and properties for dealing with dates and times, such as adding some days to an existing date, or calculating the difference of two times. One useful property is Date, which rounds a DateTime object down to the nearest day (12:00 am).

Dates, times and dates-with-times are all very similar, the main difference is how you format them, a date-with-time where you omit the time is just a date.

What David has suggested is that you work with the DateTime structure internally, strip any times using the Date property, sort on the DateTime, compare them and modify them as DateTime objects.

Only convert them to a string when they need to be displayed, at which point you can use methods such as ToShortDateString() or ToString(string format) to display only the date.