Time Passed

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

Cheers

Thomas


Answer this question

Time Passed

  • pfontyn

    That would be great but its .Net 2.0 and I have to use 1.1 :-(
    Thanks anyway

    Thomas

  • anisk

    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

  • Tb2006

    What if the endTime is not know. The user may stay logged in for 3hrs maybe 3.75hrs can it still be done


    Cheers

    Thomas

  • David Krmpotic

    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.

  • mohasad

    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());



  • Thaya

    well if you dont know the end time then how can you get the difference Do you mean to find the diff between the start time and current time

  • MarceloCouto

     Blkbird wrote:
    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......

     

    //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



  • Jason Olson

    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

    Cheers

    Thomas

  • zot166249

    Yes... how can the original solution solve this Your original solution was:

    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



  • DaveyCur

    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


  • p.b.a

    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());

    }



  • Gubi

    well the solution given initially can do this. This "system" you are referring to, is this your own application

  • ALFKI

    Hi,

    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.








  • sctwest

    For some reason this strill gives me some strange and large(or rather small) negative number.
    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

  • Time Passed