Is there any way to determine the amount of time that has passed from one specific point to another I.E. If the user logs in at 16:00 and logs out at 21:00 is there any way to tell that 5hours have passed
You can always use the StopWatch class that is part of the Diagnostics namespace.
You can then use a static instamce that you can use to determine how time has elapsed. Using an object like the StopWatch enables you to separate your application concerns. Your form should not have the responsability of calculating elapsed time.
For exemple:
public class Application { public static StopWatch Chronometer; }
//When user logs in Application.Chronometer.Start();
//When user logs out Application.Chronometer.Stop();
//When you request the logged in duration Timespan elapsedTime = Application.Chronometer.Elapsed;
No more manipulation of DateTime objects, no more risk of error.
I'm trying to pass startShift from form2 to form1 but I keep getting a marshal-by-reference error. I've declared public DateTime startTime = new DateTime(); as a global variable in form2. In form1 I create an instance of form2 in form1 where I have my endTime is created when the click event of a button is activated. when i try to use f2.startTime.ToString() it gives me this by-reference error, and when I create a TimeSpan diff = startTime - endTime I get a strange and long negative number.
Any thoughts
Thomas
After about 3-5min the diff = -732617.23:05:07.6562500
I created a string HoursOn = diff.Hours.ToString(); and after exactly 10min it gave -23 hours.
not sure I follow but lets go through this. you have 2 forms. Form2 will have the starttime whilst form1 will be the main form where the time will be calculated. So......
You will get the end time after the user "Logs off" the system. Here is how it would go: 1) User logs on....@ 1300hours {startTime = DateTime.Now = 1:00pm} 2) User does his/her thing runs the program 3) User logs off the system @ 1524hours {endTime = DateTime.Now = 3:24pm}
The startTime is not recorded until the user Logs in and is authenticated. The endTime is not recorded until the user enacts the click event of the Logout Button.
They could be on the system for 20min or 24hours. I just need a way to output the time spent on the system
I'm trying to pass startShift from form2 to form1 but I keep getting a marshal-by-reference error. I've declared public DateTime startTime = new DateTime(); as a global variable in form2. In form1 I create an instance of form2 in form1 where I have my endTime is created when the click event of a button is activated. when i try to use f2.startTime.ToString() it gives me this by-reference error, and when I create a TimeSpan diff = startTime - endTime I get a strange and long negative number.
Any thoughts
Thomas
After about 3-5min the diff = -732617.23:05:07.6562500
I created a string HoursOn = diff.Hours.ToString(); and after exactly 10min it gave -23 hours.
DateTime is a value type you don't have to "new" it. If you initialize it to its default, than all members are set to 0. Which counts as date 1.1.0001. You want startTime = DateTime.Now as initialization of startTime.
When user logs in, declare a global variable to hold the start time.
private DateTime theStartTime = DateTime.Now;
now, when application is closing, on the Form_closing event (or perhaps implement the ApplicationExit event), simply do your calculation here. You take the current DateTime and find the difference between Now (exit time) and the start time.
can you show the code you are using The solution I provided earlier should do it for you if you copy it pretty much the same except replacing the variable names and form instances
Time Passed
tackett
You can always use the StopWatch class that is part of the Diagnostics namespace.
You can then use a static instamce that you can use to determine how time has elapsed. Using an object like the StopWatch enables you to separate your application concerns. Your form should not have the responsability of calculating elapsed time.
For exemple:
public class Application
{
public static StopWatch Chronometer;
}
//When user logs in
Application.Chronometer.Start();
//When user logs out
Application.Chronometer.Stop();
//When you request the logged in duration
Timespan elapsedTime = Application.Chronometer.Elapsed;
No more manipulation of DateTime objects, no more risk of error.
Kathirvel
Thanks anyway
Thomas
Bigwave
not sure I follow but lets go through this. you have 2 forms. Form2 will have the starttime whilst form1 will be the main form where the time will be calculated. So......
//form1: //declare globally
private Form2 theSecondForm = null;
private DateTime theStartTime = DateTime.Now;
//some button click to open form2:
using (this.theSecondForm = new Form2())
{
this.theSecondForm.ShowDialog();
this.theStartTime = this.theSecondForm.startTime;
}
//form2:
//global:
public DateTime startTime = DateTime.Now;
//back to form 1: form closing event:
private void Form1_Closing(object sender, FormClosingEventArgs e)
{
DateTime stopTime = DateTime.Now;
TimeSpan theDiff = stopTime - this.theStartTime;
}
try this, does this work remember, you calculate the endTime - startTime since endtime is the latest current datetime
WIreD 0x90
AFTIadmin
Cheers
Thomas
Pockey
Here is how it would go:
1) User logs on....@ 1300hours {startTime = DateTime.Now = 1:00pm}
2) User does his/her thing runs the program
3) User logs off the system @ 1524hours {endTime = DateTime.Now = 3:24pm}
The startTime is not recorded until the user Logs in and is authenticated. The endTime is not recorded until the user enacts the click event of the Logout Button.
They could be on the system for 20min or 24hours. I just need a way to output the time spent on the system
Cheers
Thomas
George2
Vaish
Any ideas why
I've tried using .Duration().Hours, .Duration().TotalHours, .Duration().Minutes, .Duration.().TotalMinutes, .Hours, .TotalHours, .Minutes, .TotalMinutes, .ToString()
It alwyas returns strange numbers not just like .25 or 1
Cheers
Thomas
kins_z
DateTime startTime = DateTime.Now;
DateTime endTime = startTime.AddHours(3);
TimeSpan diff = endTime - startTime;
MessageBox.Show(diff.ToString());
I can't do this if the Number of hours is unknow.
Cheers
Thoams
Danny_W
Any thoughts
Thomas
After about 3-5min the diff = -732617.23:05:07.6562500
I created a string HoursOn = diff.Hours.ToString(); and after exactly 10min it gave -23 hours.
TommyG73
take a look at the TimeSpan class, this will do this for you
http://msdn2.microsoft.com/en-us/library/system.timespan(VS.80).aspx
Example:
DateTime startTime = DateTime.Now;
DateTime endTime = startTime.AddHours(3);
TimeSpan diff = endTime - startTime;
MessageBox.Show(diff.ToString());
KostyaX
DateTime is a value type you don't have to "new" it. If you initialize it to its default, than all members are set to 0. Which counts as date 1.1.0001. You want startTime = DateTime.Now as initialization of startTime.
--
SvenC
Marcos Bertoldi
ok so this is for your application.
When user logs in, declare a global variable to hold the start time.
private DateTime theStartTime = DateTime.Now;
now, when application is closing, on the Form_closing event (or perhaps implement the ApplicationExit event), simply do your calculation here. You take the current DateTime and find the difference between Now (exit time) and the start time.
private void form_closing(object sender, FormClosingEventArgs e)
{
DateTime endTime = DateTime.Now;
TimeSpan diff = this.theStartTime - endTime;
MessageBox.Show("logged in for : " + diff.ToString());
}
KeithFranklin