Phreak Posted June 11, 2003 Posted June 11, 2003 What I'm trying to do is sort through an IIS log file, and add up the total time a user has spent on the site. Each log file is separated by days, and everytime a user opens a new page, the time is logged. So I can have multiple records for the same user with different times. Say for instance they opened "this" page at 10:30:15, and then opened another page at 10:31:25, and another at 10:33:40. How can I add all of these times together to get the TOTAL time a user spent on the site? For instance, the total time the user used in the above example spent 3 min 25 sec on the site. Is there a way to do that? Maybe even easier, put in the 1st time and the last time logged and have it spit out the whole time span? Quote If it works... don't worry, I'll fix it.
Phreak Posted June 11, 2003 Author Posted June 11, 2003 Ok, I found the DateDiff function, but it keeps returning the value '0'. Here is what I have: tTTime = DateDiff(DateInterval.Hour, CType(tSTime, Date), CType(tETime, Date)) I did a debug and checked out the values of the variables when it gets to this line and the values are: tSTime = 14:25:45 tETime = 14:27:40 So there is a difference. Can anyone please help? I am suppose to have this figured out by the end of the day tomorrow. Quote If it works... don't worry, I'll fix it.
Leaders dynamic_sysop Posted June 11, 2003 Leaders Posted June 11, 2003 MsgBox(DateDiff(DateInterval.Hour, Date.Now, Convert.ToDateTime("11/06/2003 21:01:33"))) in your case putting the times as ness. but this only calculates the difference in hours not the minutes / seconds so you'll get 0 with the 2 times you got there. this will give you the diff for seconds : Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click Dim tSTime As String = "14:25:45" Dim tETime As String = "14:27:40" MsgBox(DateDiff(DateInterval.Second, Convert.ToDateTime(tETime), Convert.ToDateTime(tSTime))) End Sub this for minutes : Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click Dim tSTime As String = "14:25:45" Dim tETime As String = "14:27:40" MsgBox(DateDiff(DateInterval.Minute, Convert.ToDateTime(tETime), Convert.ToDateTime(tSTime))) End Sub and they will sort you out with your problem. Quote
*Experts* Volte Posted June 11, 2003 *Experts* Posted June 11, 2003 You shouldn't use the DateDiff function, as it is left over from VB6 for compatibility. You should use the Date and TimeSpan classes to do date arithmetic.Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim time1 As Date = Date.Parse("14:25:45") Dim time2 As Date = Date.Parse("14:27:40") ' subtract time2 from time1 Dim difference As TimeSpan = time2.Subtract(time1) MessageBox.Show("The difference is: " & difference.ToString) End SubTimeSpan objects are similar to Date object, but they are used to hold a length of time, rather than an actual representation of a time. Quote
Phreak Posted June 12, 2003 Author Posted June 12, 2003 Thanks! TimeSpan worked exactly how I wanted. I really appreciate the help! Quote If it works... don't worry, I'll fix it.
Leaders dynamic_sysop Posted June 14, 2003 Leaders Posted June 14, 2003 i found that DateDiff doesnt work in C# ( although you can implement it through a lot of code ) so i thought i'd have a mess with TimeSpan , here's a working example for calculating differences of date / time in C# with timespan : private void button1_Click(object sender, System.EventArgs e) { try { string strDate="10/06/2003 10:20:15"; string strFinish="11/06/2003 18:28:37"; string MsgDay="Difference in Days:\n"; string MsgHour="\nDifference in Hours:\n"; string MsgMins="\nDifference in Minutes:\n"; TimeSpan s=Convert.ToDateTime(strFinish).Subtract(Convert.ToDateTime(strDate)); MessageBox.Show(MsgDay+s.TotalDays.ToString()+MsgHour+s.TotalHours.ToString()+MsgMins+s.TotalMinutes.ToString()); } catch { System.Exception f = new System.Exception(); MessageBox.Show(f.Message); } } Quote
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.