Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Can anyone help me with the following problems please???

 

1) I want to get the current time, which I have managed to do but it gives me a strange value, can anyone help me get rid of the decimal place on the seconds??

 

varDateTime = Now()
Dim strTime As String = varDateTime.TimeOfDay.ToString

 

but it give me the following 23:43:31.9124704 when I just want 23:43:32

 

2) Also how can I get the difference in time between to times (both values are stored as string variables)

 

e.g:

 

dim date1 as string = "23:43:32"

dim date1 as string = "23:42:32"

 

then it would return a value of 1 mins or 00:01:00

 

Thanks inadvance

 

Simon

Posted

To get the time now as a string use:

 

Dim strTime As String = Date.Now.ToLongTimeString

 

To get the differnce between 2 times as strings use:

 

dim date1 as string = "23:43:32"

dim date1 as string = "23:42:32"

dim difference as string=New Date(0).Add(Date.Parse(date1).Subtract(Date.Parse(date2))).ToLongTimeString

 

:)

Please check the Knowledge Base before you post.

"Computers are useless. They can only give you answers." - Pablo Picasso

The Code Net

Posted

mark007,

 

When I try the code that you recomended it gives me the following error:

 

Ticks must be between DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks.

 

Any ideas???

 

Thanks

 

Simon

  • Administrators
Posted (edited)

For #1 try

Dim s As String
s = Date.Now.ToString("T")

 

and for #2

Dim d1, d2 As Date
d1 = Date.Parse("23:43:32")
d2 = Date.Parse("23:42:32")

Dim ts As TimeSpan
ts = d1.Subtract(d2)

MessageBox.Show(ts.TotalSeconds.ToString)    'or whatever units you require

 

although if you have the dates as a proper Date variable rather than a string you could just subtract the dates direct and format the timespan output to meet your requirements rather than converting to strings and then back again.

Edited by PlausiblyDamp

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...