lidds Posted July 27, 2005 Posted July 27, 2005 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 Quote
mark007 Posted July 28, 2005 Posted July 28, 2005 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 :) Quote Please check the Knowledge Base before you post. "Computers are useless. They can only give you answers." - Pablo Picasso The Code Net
lidds Posted July 28, 2005 Author Posted July 28, 2005 Thanks, for your help will try it later tonight. Quote
lidds Posted July 28, 2005 Author Posted July 28, 2005 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 Quote
Administrators PlausiblyDamp Posted July 28, 2005 Administrators Posted July 28, 2005 (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 July 28, 2005 by PlausiblyDamp Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.