How can i convert my column with timespan dataType (Eg: 04:04:45 ) to DateTime in HH:mm:ss format (Eg: 04:04:45)?

Currently I can convert the column to datetime (Eg: 10/2/2012 4:04:45 AM) but I only want the time (Eg: 04:04:45 ).

Can someone help me please...

Recommended Answers

All 8 Replies

Set the format to long time.


Dim dat As New Date
dat = #10/2/2012 4:04:45 AM#
Dim sampledateformat As Date
sampledateformat = Date.Now
TextBox1.Text = Format(sampledateformat, "hh:mm:ss tt")

I hope this sample code will help you solve your problems.... ^_^


dim myDate as Date = Now

textbox1.text = timevalue(myDate)

MSDN TimeValue Function

Thanks all for your reply but it still couldnt solve my problem. I tried all of your solution but it still appear with date and time.

Dim time As DateTime

For i As Integer = 0 To grpDT.Rows.Count - 1 
              time = grpDT.Rows(i).Item("DURATION").ToString() 
              grpDT.Rows(i).Item(datanew) = time.ToShortTimeString() 

'' *grpDT is my datatable *''
'' *Column "Duration" is in Timespan type *''
'' *datanew is a new column i created to store new value from Duration column *''
'' *datanew is in datetime format *''

If I change the code to this:

    Dim time As DateTime

    For i As Integer = 0 To grpDT.Rows.Count - 1 
       time = grpDT.Rows(i).Item("DURATION").ToString() 
       grpDT.Rows(i).Item(datanew) = time.ToShortTimeString("HH:mm:ss") 

I got error like below:

[FormatException: Input string was not in a correct format.]
   Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String Value, NumberFormatInfo NumberFormat) +201
   Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String Value) +66

[InvalidCastException: Conversion from string "HH:mm:ss" to type 'Integer' is not valid.]
   Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String Value) +246
   WebApplication3._Default.Button1_Click(Object sender, EventArgs e) in D:\reporting\try\WebApplication3\WebApplication3\Default.aspx.vb:113
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565

What's wrong with my code? Can someone help me please..

U want to change the datatype in database right??? which database are u using???

No. I'm using Oracle but I dont want to change anything inside my database.
I get the data that I want from Oracle and store it dataTable.
I just want to copy the value in Duration column, store in new column named datanew as datetime.

I'm not 100% sure about Oracle but I know if you set a field up as a datetime in SQL Server for instance, if you pass in a time value it will automatically append todays date and if you only pass in a date it will automatically append a time value of "00:00:00" i.e. midnight.

I suggest if you wish to store only a time value you change the field's datatype to a string of some description.

 Dim a As Date = Now

        Dim b As String
        b = Format(a, "hh:mm:ss")

you can get idea from this.. just an idea ^^

Be a part of the DaniWeb community

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