Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
i need to calculate between two times from pm to am
i did this code but i face problem from pm to am because it's another day,
  	
                        DateTime date1 = System.Convert.ToDateTime(DTimeBeginWork.Value);	
			DateTime date2 = System.Convert.ToDateTime(DTimeEndWork.Value);	
			TimeSpan ts = new TimeSpan();
			ts = date2.Subtract(date1);
			
//			DgNHours.DigitText = ((int)(ts.Hours)).ToString();
			DgNHours.DigitText = (ts.Hours.ToString());
			DgNMinutes.DigitText = (ts.Minutes.ToString());
 

thank u
Posted 30-Jun-13 17:56pm
Comments
Sergey Alexandrovich Kryukov at 30-Jun-13 23:02pm
   
It looks like you question lacks on word: calculate what between two times? Duration, or what? What's the role of AM/PM? What's the problem?
 
Do you understand that it cannot make any difference: this is a matter of time formatting, not the time structure itself.
—SA
Atif BOUZAGLAOUI at 30-Jun-13 23:12pm
   
i need to calculate hours and minutes; also when from pm to am it means the time will enter other day , so i cant enter and show me minus
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

As long as you are working with DataTime you should be OK - so I guess your DTimeBeginWork contains only the time in some format and not the date?
 
That being the case, you either need to specify the date or assume the date is the next day if the endwork time value is less than the Beginwork time value...
 
e.g.
 
if (DTimeBeginWork.Value > DTimeEndWork.Value)
{
    date2.AddDays(1);
}
  Permalink  
Comments
Atif BOUZAGLAOUI at 30-Jun-13 23:18pm
   
i need to calculate hours and minutes; also when from pm to am it means the time will enter other day , so when it enter and it show minus and it be back
Atif BOUZAGLAOUI at 30-Jun-13 23:18pm
   
i tried yyour code but nothing change
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi !
 
TimeSpan t = date1 - date2;
 
with t you can translate result to Hour, Minute, Seccond, day, month, year through properties same as type.
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 1-Jul-13 0:58am
   
Actually, date2 - date1. OP have actually done an equivalent operation, so it won't resolve the problem (even though using '-' operator is much more readable). I think the solution of the problem would be... coming to understanding that the problem does not really exist.
—SA

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

  Print Answers RSS
0 OriginalGriff 195
1 ProgramFOX 130
2 Maciej Los 105
3 Sergey Alexandrovich Kryukov 105
4 Afzaal Ahmad Zeeshan 82
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web01 | 2.8.1411022.1 | Last Updated 1 Jul 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