-1

I am trying to load some dates from a database, but i dont want to create a datetime object with time, i only want the date. I have tried with var date = value.Date; it doesn't work. I have seen a lot of methods that basically format the date to show it as a string and i don't want to do that, because than i have to load all the data into the grid manually. I hope someone can help me. Here is the code

DateTime startDate = (DateTime)row["Course_StartDate"];
DateTime endDate = (DateTime)row["Course_EndDate"];
zomlaaa95
  • 73
  • 10
  • 1
    The `DateTime` class has a time property. There is no way to change that. – Matt Rowland Jan 05 '17 at 20:40
  • 3
    Its called `DateTime` because it always has a Date and a Time. use `startDate.Date` to work with just the date. Depending on what `the grid` is, you might also be able to enter a display style – Ňɏssa Pøngjǣrdenlarp Jan 05 '17 at 20:40
  • That's what I was afraid of... So i have to do it manually? – zomlaaa95 Jan 05 '17 at 20:41
  • This is not possible. The closest you get is having hour, minute and seconds set to 0. The struct itself always contains fields for the time. – Bauss Jan 05 '17 at 20:41
  • Do *what* manually? – stephen.vakil Jan 05 '17 at 20:41
  • No. Because you're setting `startDate` and `endDate` as `DateTime` variables, they will always have a time. To use just the date, you can leave your code as-is, and moving forward you can just use `startDate.Date` and `endDate.Date` instead of `startDate` and `endDate`. – Tyler Roper Jan 05 '17 at 20:41
  • Plutonix i've tried that, doesn't work. Still has time – zomlaaa95 Jan 05 '17 at 20:41
  • Join the date parts together as a `string` object as per this [MSDN Example](https://msdn.microsoft.com/en-us/library/dca21baa%28v=vs.100%29.aspx?f=255&MSPPError=-2147217396) - If you wish to keep accuracy, strip the hour/min/ss/etc, and use the UTC timestamp in seconds. – Kraang Prime Jan 05 '17 at 20:42
  • It will **have** a time but there are oodles of ways to display **just** the date. As I said, it depends on what "the grid" actually is – Ňɏssa Pøngjǣrdenlarp Jan 05 '17 at 20:43
  • it is a ordinary data grid, but it's item source is an ICollectionView – zomlaaa95 Jan 05 '17 at 20:45
  • @zomlaaa95 to get just the date portion of a datetime variable you have to use a string value of the date. If the time portion is not needed, as in down to the minute calculations, then displaying just the string value should not be an issue. You can do this in the prerender event of the data grid – Paul Stoner Jan 05 '17 at 20:58
  • See also: [A type for Date only in C# - why is there no Date type?](http://stackoverflow.com/q/5314309/634824) – Matt Johnson-Pint Jan 05 '17 at 22:49
  • And as my "answer" said, do checkout NodaTime http://nodatime.org , which is mentioned in the page that @MattJohnson commented on. Happy trails. – STLDev Jan 05 '17 at 23:40

2 Answers2

3

Do you need to use any of the functionality of DateTime, such as comparing dates or adding/subtracting days from a date? If not, you can probably just make your variables strings instead of DateTimes:

string startDate = ((DateTime)row["Course_StartDate"]).ToShortDateString();
string endDate = ((DateTime)row["Course_EndDate"]).ToShortDateString();

Edit: Since you do need to compare times, you could use a second property that returns the equivalent value as you need it:

DateTime startDate = new DateTime();
DateTime endDate = new DateTime();
string startDateString
{
    get
    {
        return startDate.ToShortDateString();
    }
}
string endDateString
{
    get
    {
        return endDate.ToShortDateString();
    }
}

private void LoadMethod()
{
    startDate = (DateTime)row["Course_StartDate"];
    endDate = (DateTime)row["Course_EndDate"];
}

Then you can do your math with the DateTime objects, and use the string objects in your table.

Dave Smash
  • 2,941
  • 1
  • 18
  • 38
1

If you are talking about DataGridView, change the Column Format (DefaultCellStyle > Format) to MM/dd/yyyy

Gordon Bell
  • 13,337
  • 3
  • 45
  • 64