Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date Time Format In ComboBox 1

Status
Not open for further replies.

Jables

Programmer
Aug 28, 2001
148
US
I am using a ComboBox to display time values in a DropDownList. The values get pulled from a SQL Server database. The field in the database is a datetime field and I enter the values like "6:00:00 AM"

When I pull these values from the database and use them to populate the ComboBox, they display like "12/30/1899 6:00:00 AM"

How do I format them to display as a short Time?
 
If you're populating it manually (no bound controls) you might try this:

Box.Items.Add(TimeValue(Time Goes Here))

If the box is bound to the database, I'm not sure how to do it.
 
Well, I am populating it with a DataView. Maybe if anyone knows how to force a column in the dataview to format as a Short Time, that would solve my problem.
 
In Your SQL query you can convert the DateTime to a varchar and then apply the style.

Code:
Select Convert(varchar,DateColumn,108) from my table

The problem is that SQL does not have a time datatype so if no date is specified it uses the default date as configured. if you want just times you may be better off storing the data as a varchar and then Cast it to a date time or time format when you need to work with it as a time.


"Shoot Me! Shoot Me NOW!!!"
- Daffy Duck
 
Thanks MDXer, this solves my problem, but I don't see any way to make it not display in 24-hour time. If it's not possible, I'll just make another varchar column alongside the time value in the 12-hour format, bring it into the view, and use that as the display member for the ComboBox.
 
sorry without embedding looping logic to check > 12 so you can subtract 12 this is as good as it gets.

"Shoot Me! Shoot Me NOW!!!"
- Daffy Duck
 
Code:
DECLARE @dt DATETIME
SET @dt = '3/11/2003 11:32:00 PM'
SELECT REPLACE(REPLACE(CONVERT(VARCHAR, @dt, 109),REPLACE(CONVERT(VARCHAR, @dt, 107), ',', '') , ''), ':000', ' ')
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top