-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"]; 
13
  • 1
    The DateTime class has a time property. There is no way to change that. Commented Jan 5, 2017 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 Commented Jan 5, 2017 at 20:40
  • That's what I was afraid of... So i have to do it manually? Commented Jan 5, 2017 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. Commented Jan 5, 2017 at 20:41
  • Do what manually? Commented Jan 5, 2017 at 20:41

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

i do need comparation so i can't set it as string
1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.