Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 325 Vote(s) - 3.58 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I get the time difference between two DateTime objects using C#?

#1
How do I get the time difference between two `DateTime` objects using C#?
Reply

#2
What you need is to use the [DateTime][1] classs Subtract method, which returns a [TimeSpan][2].

var dateOne = DateTime.Now;
var dateTwo = DateTime.Now.AddMinutes(-5);
var diff = dateTwo.Subtract(dateOne);
var res = String.Format("{0}:{1}:{2}", diff.Hours,diff.Minutes,diff.Seconds));


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#3
You need to use a [TimeSpan][1]. Here is some sample code:

TimeSpan sincelast = TimeSpan.FromTicks(DateTime.Now.Ticks - LastUpdate.Ticks);


[1]:

[To see links please register here]

Reply

#4
IF they are both UTC date-time values you can do `TimeSpan diff = dateTime1 - dateTime2;`

Otherwise your chance of getting the correct answer in every single possible case is zero.
Reply

#5
private void button1_Click(object sender, EventArgs e)
{
TimeSpan timespan;
timespan = dateTimePicker2.Value - dateTimePicker1.Value;
int timeDifference = timespan.Days;
MessageBox.Show(timeDifference.ToString());
}
Reply

#6
The way I usually do it is subtracting the two DateTime and this gets me a TimeSpan that will tell me the diff.

Here's an example:

DateTime start = DateTime.Now;
// Do some work
TimeSpan timeDiff = DateTime.Now - start;
timeDiff.TotalMilliseconds;
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through