?
Solved

Format time in VB.NET

Posted on 2011-10-29
12
Medium Priority
?
660 Views
Last Modified: 2012-06-21
I store time in a Sql database as the TIME datatype. When I display the time in a DataGridView, it is formatted as military time. I can't seem to get it to convert to the 12 hour clock instead of the 24 hour. How can I do this?
0
Comment
Question by:fieldb1
12 Comments
 

Author Comment

by:fieldb1
ID: 37053755
Thanks, but they do not resolve the issue.

I am working with the Time datatype in sqlserver. That translates to the TimeSpan datatyp in .NET. However, when I display this in a DataGridView, the time is 24 hours instead of the 12 hour that I want. How can I convert the TimeSpan value to 12 hour?
0
 
LVL 13

Accepted Solution

by:
khairil earned 2000 total points
ID: 37053797
Hi,

You can try this in your SQL query.

SELECT CONVERT(varchar(15),@yourtime,100) 

Open in new window


Replace @yourtime with time variable or column. If it's in datetime(2) format, then you will have to cut out the date portion from it after running the CONVERT function.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37053972
>I am working with the Time datatype in sqlserver. That translates to the TimeSpan datatyp in .NET.

The time data type

Defines a time of a day.

Whereas a TimeSpan

Represents a time interval.

So they are two different things.


Try

Select Convert(DateTime, @yourtime, 108)
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37053975
Also try 114 in place of 108
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37053978
0
 

Author Comment

by:fieldb1
ID: 37054362
The data is of the Time data type in sql. My issue is dealing with this data type in .net. Are you sating that the best way to do that is to convert to DateTime? I want to deal with the data type directly.
0
 

Author Comment

by:fieldb1
ID: 37054365
The data is of the Time data type in sql. My issue is dealing with this data type in .net. Are you sating that the best way to do that is to convert to DateTime? I want to deal with the data type directly.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37054374
> My issue is dealing with this data type in .net

You can deal with it as in displaying it but if you want to manipulate it (change format) then I have shown you one way of doing it which I dont think would convert it to datetime.
0
 

Author Comment

by:fieldb1
ID: 37054775
So the Time sql data type does not translate well into .net?
0
 

Author Comment

by:fieldb1
ID: 37054797
The CONVERT(DateTime, @myTime, 108) doesn't change the time format to 12 hour clock. It still displays in the management studio as 24 hour. 114 doesn't work either. In the ADO.NET, coding the CONVERT  in my Select statement broke the function.
0
 

Author Closing Comment

by:fieldb1
ID: 37054833
I coded the CONVERT(varchar, @myTime, 100) into the ado and it is working nicely. Thank you.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question