Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP .NET
In my project i have to save the time taken to solve a sum. If the sum did was wrong then the student have to done it again. In this time i have to add my time with the previous time which i saved in our database..For Example in the first attempt time taken was 00:15:25 (hr:min:sec format),second attempt it is 00:10:25. Then in my database i have to save it as 00:25:50 (adding first and second attempt time)
 
Right now i'm trying to split the time and getting values separately(ie, taking hr,min,sec time separately and then adding it separately.. Is there any other method.. can i do with jquery or javascript..
Posted 30-Jan-13 0:58am
shamjid1.5K
Edited 25-Feb-13 1:40am
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

The best way is to start with the start time, then find the end time.
DateTime startTime = DateTime.Now;
...
DateTime endTime = DateTime.Now;
If you then subtract them, you get a Timespan:
TimeSpan takenSoFar = endTime - startTime;
 
When it comes to the second attempt, you do the same thing:
DateTime startTime2 = DateTime.Now;
...
DateTime endTime2 = DateTime.Now;
If you then subtract them, you get a Timespan:
TimeSpan takenThisAttempt = endTime2 - startTime2;
 
You can then add the two Timespans together:
takenSoFar += takenThisAttempt;
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

You can use TimeSpan
 
TimeSpan t = TimeSpan.FromSeconds(seconds);
 
and
 
use t.Hours, t.Minutes and t.Seconds to format the string how ever you want.
  Permalink  
Comments
shamjid at 26-Feb-13 2:13am
   
@Maksud:-I can't get u???could u plz explain using code...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
 
Use the following code.
 
TimeSpan time1 = new TimeSpan(0, 15, 25);
            TimeSpan time2 = new TimeSpan(0, 25, 50);
            TimeSpan time = time1.Add(time2);
  Permalink  
v4

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

  Print Answers RSS
0 Marcin Kozub 265
1 Richard MacCutchan 239
2 OriginalGriff 208
3 Sergey Alexandrovich Kryukov 185
4 Praneet Nadkar 167
0 OriginalGriff 7,913
1 Sergey Alexandrovich Kryukov 7,232
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,865


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 25 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100