I have stored time in an access database with a format of MediumTime, which displays in access as I want it, such as 7:30 AM. However, when I retrieve the time in my c# program, it also has a date (which is the wrong date anyway since I didn't enter one in access).

queryString = " SELECT Time FROM database";

I can change the time to a string and parse just the time, but then when I sort the time (as a string) it doesn't sort in the correct order. I've also tried using DateTime.ParseExact, but can't seem to get that to work.


Recommended Answers

All 5 Replies

another approach maybe? I am putting this data into a datagrid. Is there a way to change the column format to just display the time and not the date?

Don't know if this will help you but you can get the Time of a DateTime using the TimeSpan

DateTime data = new DateTime(2007, 11, 10, 13, 15, 0);
            TimeSpan time = data.TimeOfDay;
            //  time.ToString() returns "13:15:00"


Thanks. That code works great to get a string, but not quite what I was trying to do.

I have the users enter a time (no date required) that is stored in an Access database. When I retrieve it (using SQL) the format is DateTime and looks something like ...

{08/10/1899 1:15:00 PM}

The time is good, but the date is a nonsense date. I wrote the following

(dataTable is the table I retrieved from the database containing the time)

            char[] splitter = { ' ' };
            string[] arInfo = new string[4];

            tempDate = dataTable.Rows[i][0].ToString(); //0th column is datetime

            arInfo = tempDate.Split(splitter);
            arInfo[1] += " ";
            newDate = String.Concat(arInfo[1], arInfo[2]);

This gets me a string with just the time ... although your way is much more straightforward. The problem I have is that I've bound the dataTable to a dataGrid, so the string doesn't help alot. I guess I need to replace the dataTable with the string instead of a DateTime.

The other thing I am pursueing is something in the SQL command to parse or cast the values such as

SELECT  TIMEOFDAY(Appointments.Time)  FROM ....

but that hasn't seemed to work either.

Thanks again for your response.

of course use: {0:t}

Unfortunately, no matter what you can do you can't get just a time value from the database. Shaulf's suggestion will work, but that's for a string.

Here's a suggestion, though. If you're working with Time, there may end up being a time where you deal with something that happens at 11:59PM and ends at 12:30AM the next day. If you just store the time values, how will you deal with this? Just a thought. Then it starts to make sense why there's a DateTime value instead of just a Time value...

But also, I think you can cast a SqlDataType that is a Date/Time value to a DateTime data type. From there, the closest you might be able to get is use the .ToShortTime() method or something just to get the time. But then again, it only returns it in a string, too...

The DateTime object has all kinds of properties, like Hours, Minutes, Seconds, etc, that you can use in some form or another if you're wanting to do things like Time adding or similar. I suggest you look into those...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.