hi,
I am doing one attendance project using RFID Card. I am using c# windows application. Now I want to know how to calculate time difference between yesterday's time and today's time.
I want to get yesterday's time(time) from database and get today's time from system time(currenttime).
I use this coding,
double duration = (currenttime).Subtract(time).TotalSeconds;
This coding calculate total difference in seconds with negative value...
Please help...
thanks in advance.....
Loading
Sheheryar AliPosted Sep 24, 2022, 10:11 AM
Below is my code i wrote for myself, to find the difference between current time and given time, which return the difference in miliseconds, i use it for timer from current time to any upcoming time i write in 24 hours format
using System;
class Program
{
static void Main() {
double diff = timeDifferenceInMilliseconds(16,0,0);
System.Console.WriteLine("Difference recieved: "+ diff);
}
public static double timeDifferenceInMilliseconds(int hours, int minutes, int seconds){
DateTime currentTime = DateTime.Now;
DateTime myTime = new DateTime(currentTime.Year, currentTime.Month, currentTime.Day, hours,minutes,seconds);
TimeSpan timeDifference = myTime.Subtract(currentTime);
return (timeDifference.TotalMilliseconds < 0) ? timeDifference.TotalMilliseconds+24*3600000 : timeDifference.TotalMilliseconds;
}
}
leathu rajPosted Jun 26, 2014, 1:36 AM
Lakshmanan Sethu SankaranarayanPosted Jun 25, 2014, 5:57 AM
Refer this
http://nishantwork.wordpress.com/2012/11/01/how-to-get-two-time-difference-in-minute-in-c-or-net/
Sagar PardeshiPosted Jun 25, 2014, 4:34 AM
Guest UserPosted Jun 25, 2014, 1:17 AM
To get the difference time between 2 time you can use the following code:
var dtYesterday = DateTime.Now;
var dtNow = DateTime.Now;
TimeSpan diff = dtNow - dtYesterday;
And use the difference time in following format:
int days = diff.Days;
int hour = diff.Hours;
int min = diff.Minutes;
int sec = diff.Seconds;
int milliSec = diff.Milliseconds;