(Advertisment)
German Wear Discount Shop - Click Here
[Dot]Net-Friends
Skip Navigation Links
Home
Latest
Fast Code
Articles
Tutorials
Online Resources
Forums
Login   | Hi, Guest


Started By: Abdul_SatharReplies: 1 Views: 6156

<<Visual C# .NET
By:Abdul_Sathar
How to calculate the Time Duration from End time to Start time?
Abdul_Sathar Profile Avatar
Sep 26, 2007 08:43
Points 1

How to calculate the Time Duration from End time to Start time?

For example the start time is 2007-09-25 16:59:12 & End time is 2007-09-25 17:10:12.Please help me,how to write the C# Code for this.

By:dotnet-friends
How to calculate the Time Duration from End time to Start time?
dotnet-friends Profile Avatar
Sep 27, 2007 08:44
Points 12

The Difference of the Two Dates is Called TimeSpan in .NET.

You can measure the TimeSpan Just By taking Difference of the Two Dates.

 

/* Read the initial time. */
    DateTime startTime = DateTime.Now;
    Console.WriteLine(startTime);

    /* Do something that takes up some time. For example sleep for 1.7 seconds. */
    Thread.Sleep(1700);

    /* Read the end time. */
    DateTime stopTime = DateTime.Now;
    Console.WriteLine(stopTime);

    /* Compute the duration between the initial and the end time.
     * Print out the number of elapsed hours, minutes, seconds and milliseconds. */
    TimeSpan duration = stopTime - startTime;

    //Now you can check it how many Hours, Minutes or Secounds it Took
    Console.WriteLine("hours:" + duration.Hours);
    Console.WriteLine("minutes:" + duration.Minutes);
    Console.WriteLine("seconds:" + duration.Seconds);
    Console.WriteLine("milliseconds:" + duration.Milliseconds);

Reply to this Thread Start New Thread