•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C# section within the Software Development category of DaniWeb, a massive community of 455,967 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,741 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C# advertiser: Programming Forums
Views: 1818 | Replies: 5
![]() |
•
•
Join Date: Nov 2007
Posts: 3
Reputation:
Rep Power: 0
Solved Threads: 0
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.
Thanks
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.
Thanks
•
•
Join Date: Nov 2006
Location: Bonners Ferry, ID
Posts: 280
Reputation:
Rep Power: 3
Solved Threads: 39
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"•
•
Join Date: Nov 2007
Posts: 3
Reputation:
Rep Power: 0
Solved Threads: 0
Jerry,
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.
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.
•
•
Join Date: Dec 2003
Location: Nashville, TN
Posts: 2,333
Reputation:
Rep Power: 11
Solved Threads: 102
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...
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...
Alex Cavnar, aka alc6379
![]() |
•
•
•
•
•
•
•
•
DaniWeb C# Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- retrieve data from database into jsp(urgent)!!! (Oracle)
- How to retrieve images from database (PHP)
- require urgent help to retrieve image from database (PHP)
- Retrieve Information from Database & Display into ASP.NET Web Controls Textbox (ASP.NET)
- Populate data from database into JList (Java)
- date time issue in JSP (JSP)
Other Threads in the C# Forum
- Previous Thread: Is this possible?
- Next Thread: Passing multiple enum values as one parameter



Linear Mode