| Code level: intermediate Code area: Code Essentials Printer Friendly Version | ||
| Title: Date/time difference in hh:mm:ss format #2 | ||
| Description: This is another way to get a time difference between two times. See http://www.outlookcode.com/codedetail.aspx?id=97. | ||
| Date: 29-May-2003 18:51 | ||
| Code level: intermediate | ||
| Code area: Code Essentials | ||
| Posted by: Kent Baker | ||
| Body: |
||
| All 1comments |
| Page [ 1 ] | ||
|
|
Kent Baker
30-May-2003 01:57
Date data types are essentially double data types. A date converted to a double consists of 2 parts: the integer and the decimal. The integer value represents the number of days since 12/30/1899. The decimal value represents the time of day; divide 1 by 86400 (seconds in a day) and you get the decimal value for 1 second. The double value ran on my computer a little while ago using the Now() function produced the number 37771.0455671296; lots of days since last century and a small decimal meaning it's really early in the morning. Simply subtracting a later date from an earlier date will produce the time difference, e.g. 2.0; however, realize that the resulting number is meaningless as a date since a 2 day difference displayed as a date would be 1/1/1900. Function 1 returns a string value in 'hh:mm:ss' format similar to Sue's. To address the potential for a more than 24 hour difference (a good point brought up by Sue), the number of days is converted to an integer and multiplied by 24. Converting a double to an integer rounds the number according to normal rules; thus, subtracting 0.5 will ensure that a number like 5.74 returns 5 for the number of days instead of 6. The '0.74' represents the leftover time, so the Hour function is used to add to the number of hours calculated from the days. Then the string value of the minutes:seconds is added. Using the number 5.74 returns '137:45:36' -- 120 hrs (5 days) + 17:45:36. You can see that 0.74 is close to 0.75 which would be 3/4 of a day and that would be 6:00 PM, so we're just a little bit shy of 18:00:00. Function 2 just changes the format. The number 5.74 returns '5 days, 17:45:36'. Any kind of a string value can be returned. |
|
| Page [ 1 ] | ||
| Post your comment name email |
