Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to calculate timestamp between to two times like
08:30:00 and 17:15:00.
if any code available please forward it.
thanks in advance.
Posted
Updated 14-Aug-12 1:47am
v2

See this code as an example:
C#
private void Form1_Load(object sender, EventArgs e)
{
  DateTime tm1 = DateTime.Parse("08:30:00");
  DateTime tm2 = DateTime.Parse("17:15:00");
  TimeSpan span = tm2 - tm1;
  MessageBox.Show(span.ToString());
}

See here for more information:
http://msdn.microsoft.com/en-us/library/system.timespan%28v=vs.80%29.aspx[^]
http://msdn.microsoft.com/en-us/library/ee372286.aspx[^]
 
Share this answer
 
v2
Try this:
C#
protected void Page_Load(object sender, EventArgs e)
{
    // Declare and get DateTime values
    DateTime StartDate = System.DateTime.Now;
    DateTime EndDate = System.DateTime.UtcNow;
 
    // Find time difference between two dates
    TimeSpan TimeDifference = StartDate - EndDate;
 
    // Write difference in hours and minutes
    Response.Write("Time difference between server time and Coordinated Universal Time (UTC) is " +
        TimeDifference.Hours.ToString() + " hours ");
    if (TimeDifference.Minutes != 0)
        Response.Write(" and " + TimeDifference.Minutes.ToString() + " minutes.");
 
}

Ref.: From here[^]

difference between two time intervals[^]
Time difference between two datetime objects in ASP.NET[^]
C# Time difference calculation[^]

and more threads here[^]
 
Share this answer
 
 
Share this answer
 
Hi.
Try this:

C#
DateTime dt1 = new DateTime((long)TimeSpan.FromMinutes(30).TotalMilliseconds);
			DateTime dt2 = new DateTime((long)TimeSpan.FromMinutes(29).TotalMilliseconds);

			TimeSpan diff = dt1 - dt2;
 
Share this answer
 
C#
DateTime dt1= DateTime.Parse("08:30:00");
DateTime dt2= DateTime.Parse("17:15:00");
TimeSpan span = dt2- dt1; 

or

C#
DateTime dt1= DateTime.Parse("08:30:00");
DateTime dt2= DateTime.Parse("17:15:00");
string span = (dt2- dt1).Hours+":"+(dt2- dt1).Minutes;
 
Share this answer
 
v2
Try this

C#
DateTime time1 = DateTime.ParseExact("08:30:00", "HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);

DateTime time2 = DateTime.ParseExact("17:15:00", "HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);

TimeSpan diff = time2 - time1;


OR
C#
DateTime time1 = DateTime.Parse("08:30:00");
DateTime time2 = DateTime.Parse("17:15:00");
TimeSpan diff = time2 - time1;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900