0

I have Vb.net code like this:

Dim requestddate As DateTime = DateTime.Now   
Dim rdate As Date = requestddate.Date 
Dim rtime As TimeSpan = requestddate.TimeOfDay

In rtime varibale I want to get only time like: 03:15:10 (hour,minute,second).

But now I am getting rtime value as 03:15:10.7929313.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
user2535939
  • 97
  • 1
  • 3
  • 10
  • Why is that a problem? When you want to display the `TimeSpan`, use a format string that doesn't include the milliseconds. – Oded Jul 16 '13 at 11:25
  • I given code like this: – user2535939 Jul 16 '13 at 11:28
  • Dim rtime As TimeSpan = requestddate.TimeOfDay.ToString("hh:mm:ss") but still getting error – user2535939 Jul 16 '13 at 11:28
  • Sure you are. A `TimeSpan` is not a `string`. Use the `ToString` where you need to _display_ the `TimeSpan` value. – Oded Jul 16 '13 at 11:30
  • can u please show how i can write the code for the same – user2535939 Jul 16 '13 at 11:34
  • You are making the classic mistake of not documenting your question well enough. "but still getting error" doesn't help us help you. Some likelihood that you are not using .NET 4, check [this answer](http://stackoverflow.com/a/2456543/17034) – Hans Passant Jul 16 '13 at 11:40

3 Answers3

1

A TimeSpan contains all properties, there is no way to remove the milliseconds for instance. If you instead want to display this TimeSpan as String you can use ToString:

requestddate.TimeOfDay.ToString("hh\:mm\:ss"))
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

You can use a fact, that TimeSpan(Int32, Int32, Int32) can take more then 60 as seconds parameter value:

Dim rtime As New TimeSpan(0, 0, CInt(requestddate.TimeOfDay.TotalSeconds))
MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
0
    Dim requestddate As DateTime = DateTime.Now
    Dim rdate As Date = requestddate.Date
    Dim rtime As TimeSpan = requestddate.TimeOfDay

    Dim t As String = rtime.Hours & ":" & rtime.Minutes & ":" & rtime.Seconds
VB.NET LEARNER
  • 711
  • 5
  • 11